Grouping Regex question

G

gill.jp

I am trying to get all the names i.e: SMITH J , BALTIMORE D, JONES AL

String input = "NAME=(SMITH J) NAME=(BALTIMORE D) NAME=(JONES AL)
JUNK=(ABC);
Pattern pattern = Pattern.compile(" ");
Matcher matcher = pattern.matcher(input);

for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println("groupStr: "+ groupStr);
}

I played around with few patterns, I am unable to get all of the
names.
Anyone knows what this regex might be?

Thanks, J
 
C

Carl

I am trying to get all the names i.e: SMITH J , BALTIMORE D, JONES AL

String input = "NAME=(SMITH J) NAME=(BALTIMORE D) NAME=(JONES AL)
JUNK=(ABC);
Pattern pattern = Pattern.compile(" ");
Matcher matcher = pattern.matcher(input);

for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println("groupStr: "+ groupStr);

}I played around with few patterns, I am unable to get all of the
names.
Anyone knows what this regex might be?

Thanks, J

Something like this should get you started:

Matcher matcher = Pattern.compile("NAME=\\((.*?)\\)").matcher(input);
while(matcher.find()) {
System.out.println(matcher.group(1));
}

Hope that helps,
Carl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Embarrassing regex question 5
complex regex 1
complex regex 1
regex problem 9
Question on regular expression. 6
regex question 4
Java Regex Problem 7
RegEx problem 5

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top