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.