How to know the number of possible occurrences found by the group() method of the Matcher class?

Asked

Viewed 219 times

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

1 answer

3


According to this reply from Soen the most you can achieve before Java 9 is to create a method or use replaceAll to rely on the method String.split

Before the Java 9

You can add your class or create a class that contains a statistical method such as:

static int countMatches(Matcher matcher)
{
    int counter = 0;

    while (matcher.find())
      counter++;

    return counter;
}

To use call like that:

Matcher m = p.matcher(text);
System.out.println(countMatches(m));

Or:

Matcher m = p.matcher(text);
System.out.println(SuaClass.countMatches(m));

Or using . replaceAll to count occurrences in the string:

Matcher m = p.matcher(text);
System.out.println(m.replaceAll("\0").split("\0", -1).length - 1);

Note: \0 means "null", meaning that character will rarely exist in an occurrence, but there may be exceptions, so you can try alternatives to it.


Java 9

Java 9 added the method public Stream<MatchResult> results​()

With it you can count the Stream<MatchResult>, should look like this:

Matcher m = p.matcher(text);
System.out.println(m.results().count());
  • But ai ta using loop, I wonder if the Matcher class stores it somehow or if some equivalent class returns it to me.

  • 1

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.