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.
matcher.find()
does not find all matches, only the next match.You'll have to do the following:
Btw,
matcher.groupCount()
is something completely different.Complete example:
Handling overlapping matches
When counting matches of
aa
inaaaa
the above snippet will give you 2.To get 3 matches, i.e. this behavior:
You have to search for a match at index
<start of last match> + 1
as follows:This should work for non disjoint matches:
If you want to use Java 8 streams and are allergic to
while
loops, you could try this:Disclaimer: this only works for disjoint matches.
Example:
This prints out:
This may help: