Tony Tony - 7 months ago 89
Java Question

java regex match count

Let's say I have a file, and the file contains this:

HelloxxxHelloxxxHello


I compile a pattern to look for 'Hello'

Pattern pattern = Pattern.compile("Hello");


Then I use an inputstream to read in the file and convert it into a String so that it can be regexed.

Once the matcher finds a match in the file, it indicates this, but it doesn't tell me how many matches it found; simply that it found a match within the String.

So, as the string is relatively short, and the buffer I'm using is 200 bytes, it should find three matches. However, it just simply says match, and doesn't provide me with a count of how many matches there were.

What's the easiest way of counting the number of matches that occured within the String. I've tried various for loops and using the matcher.groupCount() but I'm getting nowhere fast.

Answer

matcher.find() does not find all matches, only the next match.

You'll have to do the following:

int count = 0;
while (matcher.find())
    count++;

Btw, matcher.groupCount() is something completely different.


Complete example:

import java.util.regex.*;

class Test {
    public static void main(String[] args) {
        String hello = "HelloxxxHelloxxxHello";
        Pattern pattern = Pattern.compile("Hello");
        Matcher  matcher = pattern.matcher(hello);

        int count = 0;
        while (matcher.find())
            count++;

        System.out.println(count);    // prints 3
    }
}