Get all groups from a regular expression match?
Please help me understand how to use Java regular expressions:
Quote:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class RegExp1 {
public static void main(String[] args) {
String testString = "first|aaaa | bbbb\n|cccc|ddddd";
Pattern pattern = Pattern.compile("^([^|]+)(\\|[^|]*)+$");
Matcher matcher = pattern.matcher(testString);
matcher.find();
int groupcount = matcher.groupCount();
System.out.println("Found "+groupcount+" groups");
System.out.println("Matcher: "+matcher);
for (int i = 1; i <= groupcount; i++) {
System.out.println("Match "+i+": "+testString.substring(matcher.start(i),matcher.en d(i)));
}
}
}
I will get "first" for Match 1 and "|ddddd" for Match 2, but how do I get the other matches for the second capturing group?