groupCount()
does not indicate how many times the pattern matched. It just tells how many capturing groups there are in the regex. If groupCount()
returns 2
, you know it's safe to access group(1)
or group(2)
, but group(3)
will raise an exception.
It makes no sense to call groupCount()
inside your while (m.find())
loop, because it never changes. It's a static property of the Pattern object, so you can call it before you make your first match. It's only useful when you don't know what regex is being used, which is fairly rare.
As the other responders have said, your problem is the greediness of the quantifier in (.*)
, and the solution is to use a non-greedy variant or a negated character class.
String tags = "<div class='bat'><div id='me'>";
Pattern r = Pattern.compile("<([^<>]*)>"); // no modifiers needed
Matcher m = r.matcher(tags);
System.out.printf("Number of groups: %s%n", m.groupCount() );
while (m.find()) {
System.out.println(m.group(1));
}
Notice that I dropped all the option