Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions: some groups missing

Tags:

java

regex

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).

like image 257
Artem Pelenitsyn Avatar asked Dec 09 '25 19:12

Artem Pelenitsyn


1 Answers

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:

  • Regular expression with variable number of groups?

Furthermore groups are a static and numbered like this:

                    0
          _______________________
         /                       \
         (PROD)|(SUM)(\\s+(\\d+))+
         \____/ \___/|    \____/| 
           1      2  |       4  |
                      \________/ 
                           3  
like image 144
aioobe Avatar answered Dec 12 '25 09:12

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!