@Test public void testGroupCountAndMore_UNEXPECTED_notMatchingGroupsAfterEndOfTargetGroupIncluded() throws Exception { String input = "1xxnnnnnnnnnn"; String regex = "(1xx(2yy)?)(3zz)?"; Matcher matcher = Pattern.compile(regex).matcher(input); assertTrue(matcher.find()); assertEquals(3, matcher.groupCount()); { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 1); assertGroup(-1, "1xx", 0, 3, mo); assertGroup(0, "1xx", 0, 3, mo); assertGroup(1, null, -1, -1, mo); assertGroup(2, null, -1, -1, mo); assertEquals(2, mo.groupCount()); } }
@Override public int groupCount() { if (groupCount == null) { int groupIndex = groupOffset + 1; while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) { groupIndex++; } groupCount = (groupIndex - 1) - groupOffset; } return groupCount; }
MatchResultWithOffset implements MatchResult { @Override public int groupCount() { if (groupCount == null) { int groupIndex = groupOffset + 1; while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) { groupIndex++; } groupCount = (groupIndex - 1) - groupOffset; } return groupCount; } }
MatchResultWithOffset implements MatchResult { @Override public int groupCount() { if (groupCount == null) { int groupIndex = groupOffset + 1; while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) { groupIndex++; } groupCount = (groupIndex - 1) - groupOffset; } return groupCount; } MatchResultWithOffset(MatchResult delegate, int groupOffset); }
MatchResultWithOffset implements MatchResult { @Override public int groupCount() { if (groupCount == null) { int groupIndex = groupOffset + 1; while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) { groupIndex++; } groupCount = (groupIndex - 1) - groupOffset; } return groupCount; } MatchResultWithOffset(MatchResult delegate, int groupOffset); @Override int start(); @Override int start(int group); @Override int end(); @Override int end(int group); @Override String group(); @Override String group(int group); @Override int groupCount(); @Override String toString(); }
MatchResultWithOffset implements MatchResult { @Override public int groupCount() { if (groupCount == null) { int groupIndex = groupOffset + 1; while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) { groupIndex++; } groupCount = (groupIndex - 1) - groupOffset; } return groupCount; } MatchResultWithOffset(MatchResult delegate, int groupOffset); @Override int start(); @Override int start(int group); @Override int end(); @Override int end(int group); @Override String group(); @Override String group(int group); @Override int groupCount(); @Override String toString();  }