1

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
Eduard Grinchenko
  • 514
  • 1
  • 6
  • 28

1 Answers1

5

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));
}
janos
  • 120,954
  • 29
  • 226
  • 236