Why does not the Matcher class return the number of groups correctly?

3

After the question " What is the difference of use between match () and find () methods of Matcher class? ", I continued testing this class to understand its operation, but I came across a strange behavior.

When trying to identify the number of groups found in the string by a certain regular expression, the return is always 0, even if there are ER occurrences in the string.

In the example below ( online ):

String text = "um2tres4cinco6sete8";

String regex = "[0-9]";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);

while(m.find()){
    System.out.println(m.group());
}
System.out.println("Total de grupos: " + m.groupCount());

The return is:

2  
4  
6  
8  
Total de grupos: 0 

No regex101 is also displayed this way.

According to the method documentation groupCount () :

  

public int groupCount ()
  Returns the number of capturing groups in this matcher's pattern.

If the function of this method is to return the total of captured groups, why does it return 0 instead of 4 in this example? Or am I misinterpreting something wrong with this method?

Q: If possible, I would like an explanation with examples.

    
asked by anonymous 03.10.2017 / 01:08

1 answer

4

The result is zero because it has no 'capturing group' in its regular expression. See the documentation for Pattern :

  

(X) X, as the capturing group

but "[0-9]" does not contain any excerpt between '(' and ')'.

Also note the documentation for group () , do not confuse with group(int) method:

  

Returns the input subsequence matched by the previous match.

that is, it returns what was found, not (necessarily) what corresponds to one of the groups.

With your example, if the expression was "([a-z]*)([0-9]*)" , the results of the first find would be:

  • group = "um2" - stretch found
  • groupCount = 2 - number of groups 1: ([a-z]*) , 2: ([0-9]*)
  • group(1) = "um"
  • group(2) = "2"
03.10.2017 / 01:15