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?
From the javadoc of Matcher.groupCount():
If you check the return value from
m.find()it returnstrue, andm.group()returnsmas, so the matcher does find a match.If what you are trying to do is to count the number of occurances of
sinmybooks.get(i).getBody(), you can do it like this: