Java RegEx Matcher.groupCount returns 0

David Homes picture David Homes · Sep 13, 2012 · Viewed 16.2k times · Source

I know this has been asked but I am unable to fix it

For a book object with body (spanish): "quiero mas dinero" (actually quite a bit longer)

My Matcher keeps returning 0 for:

    String s="mas"; // this is for testing, comes from a List<String>
    int hit=0;
    Pattern p=Pattern.compile(s,Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(mybooks.get(i).getBody());
    m.find();
    System.out.println(s+"  "+m.groupCount()+"  " +mybooks.get(i).getBody());
    hit+=m.groupCount();

I keep getting "mas 0 quiero mas dinero" on console. Why oh why?

Answer

Keppil picture Keppil · Sep 13, 2012

From the javadoc of Matcher.groupCount():

Returns the number of capturing groups in this matcher's pattern.
Group zero denotes the entire pattern by convention. It is not included in this count.

If you check the return value from m.find() it returns true, and m.group() returns mas, so the matcher does find a match.

If what you are trying to do is to count the number of occurances of s in mybooks.get(i).getBody(), you can do it like this:

String s="mas"; // this is for testing, comes from a List<String>
int hit=0;
Pattern p=Pattern.compile(s,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(mybooks.get(i).getBody());
while (m.find()) {
    hit++;
}