Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RegEx Matcher.groupCount returns 0

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?

like image 476
David Homes Avatar asked Sep 13 '12 20:09

David Homes


1 Answers

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++;
}
like image 103
Keppil Avatar answered Sep 28 '22 21:09

Keppil