Ed Grin Ed Grin - 4 months ago 8
Java Question

Regular expression takes bounds into group

I want to group some characters between special symbol bounds, but this bounds are involved into group. It is not connected with greedy and reluctant quantifier.

String str1 = "#162xd14a#";
Matcher matcher = Pattern.compile("#(.*?)#").matcher(str1);
if (matcher.matches()) {
for (int i = 0; i < matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}


Result:

#162xd14a#


But i expected:

162xd14a

Answer

From the official tutorial about Capturing Groups:

There is also a special group, group 0, which always represents the entire expression. This group is not included in the total reported by groupCount.

In the context of your example:

  • Group 0 contains the entire matched string, #162xd14a#

  • Group 1 contains the first (and only) matched group: 162xd14a

  • You have one group in your regex, therefore groupCount() returns 1

  • The loop stops before reaching group 1, because of the < condition

You probably want to change the loop condition to <= instead of <:

for (int i = 0; i <= matcher.groupCount(); i++) {
    System.out.println(matcher.group(i));
}
Comments