I have following Java code:
String s2 = "SUM 12 32 42";
Pattern pat1 = Pattern.compile("(PROD)|(SUM)(\\s+(\\d+))+");
Matcher m = pat1.matcher(s2);
System.out.println(m.matches());
System.out.println(m.groupCount());
for (int i = 1; i <= m.groupCount(); ++i) {
System.out.println(m.group(i));
}
which produces:
true
4
null
SUM
42
42
I wonder what's a null and why 12 and 32 are missing (I expected to find them amongst groups).
A repeated group will contain the match of the last substring matching the expression for the group.
It would be nice if the regexp engine would give back all substrings that matched a group. Unfortunately this is not supported:
Furthermore groups are a static and numbered like this:
0
_______________________
/ \
(PROD)|(SUM)(\\s+(\\d+))+
\____/ \___/| \____/|
1 2 | 4 |
\________/
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With