7
After the question "Why does the Matcher class not return the number of groups correctly?", it was explained that the groupCount()
actually returns the number of filter groups in the regular expression, and in the question "What is the difference in use between the Matcher() and find() methods?" I was explained two ways to identify occurrences of Regular Expression (ER) in a given string.
Still taking the example of one of these questions, through the method group()
i can recover the current occurrence in the string, and with a loop and the method find()
, i can recover all occurrences found in string:
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());
}
The return of this is:
2 4 6 8
i.e., the find()
found 4 occurrences of ER used.
My doubt is knowing how to identify the number of possible occurrences without having to resort to loops and increments with the method find()
. The class Matcher
or some class related to it has some method that returns that number of occurrences without resorting to loops or "workarounds" with iterations or the only way is to increment in a loop only?.
But ai ta using loop, I wonder if the Matcher class stores it somehow or if some equivalent class returns it to me.
– user28595
@Articuno Java 9
Matcher.results
?! I will anyway analyze the class before Java 9 and doc to see if it is possible to take the internal values, maybe extending the class there is some accessible internal property. I confirm to you.– Guilherme Nascimento
I hadn’t seen this Java 9, by the way, nor did I remember that I already had Java 9 kkk. But it seems that earlier there was nothing, I searched the matcher class and Pattern and they don’t store it, only the current position of find() in the string.
– user28595