
February 20th, 2009, 09:07 PM
|
 |
Contributing User
|
|
Join Date: Sep 2007
Location: outside Washington DC
|
|
|
Grouping has me stuck
I'm using Java's regex libraries, and I can't get grouping to work. Or at least I can't get it to work they way I want.
What I want is to match from the begining of the string up to, but not including any number of trailing semicolon characters. I expected that grouping it would let the first group be the characters I want.
But no.
Here is a code snipet:
Code:
static final Pattern pat = Pattern.compile("^(.*?);*$");
private static final String[] list = {
"abc;",
"N:Berger;Gary;;;",
"EMAIL;type=INTERNET;type=pref:halberman@alum.mit.edu"};
private void bar(String arg) {
Matcher m = pat.matcher(arg);
int count = 0;
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
System.out.println(arg.substring(m.start(), m.end()));
for (int i = 0; i < m.groupCount(); i++) {
System.out.println(m.group(i));
}
}
}
Any pointers greatly appreciated.
|