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();  }
@Test public void testGroupCountAndMore_UNEXPECTED_emptyGroupsAfterEndOfTargetGroupIncluded() throws Exception { String input = "1xxnnnnnnnnnn"; String regex = "(1xx())()"; 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, "", 3, 3, mo); assertGroup(2, "", 3, 3, mo); assertEquals(2, mo.groupCount()); } }
@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()); } }
@Test public void testGroupCountAndMore() throws Exception { String input = "1234567890123451xx2yy5aa6bb"; String regex = "(1xx(2yy((4zz)|(5aa)))(6bb))"; Matcher matcher = Pattern.compile(regex).matcher(input); assertTrue(matcher.find()); assertEquals(6, matcher.groupCount()); { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 0); assertGroup(-1, "1xx2yy5aa6bb", 15, 27, mo); assertGroup(0, "1xx2yy5aa6bb", 15, 27, mo); assertGroup(1, "1xx2yy5aa6bb", 15, 27, mo); assertGroup(2, "2yy5aa", 18, 24, mo); assertGroup(3, "5aa", 21, 24, mo); assertGroup(4, null, -1, -1, mo); assertGroup(5, "5aa", 21, 24, mo); assertGroup(6, "6bb", 24, 27, mo); assertEquals(6, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 1); assertGroup(-1, "1xx2yy5aa6bb", 15, 27, mo); assertGroup(0, "1xx2yy5aa6bb", 15, 27, mo); assertGroup(1, "2yy5aa", 18, 24, mo); assertGroup(2, "5aa", 21, 24, mo); assertGroup(3, null, -1, -1, mo); assertGroup(4, "5aa", 21, 24, mo); assertGroup(5, "6bb", 24, 27, mo); assertEquals(5, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 2); assertEquals("2yy5aa", input.substring(18, 24)); assertGroup(-1, "2yy5aa", 18, 24, mo); assertGroup(0, "2yy5aa", 18, 24, mo); assertGroup(1, "5aa", 21, 24, mo); assertGroup(2, null, -1, -1, mo); assertGroup(3, "5aa", 21, 24, mo); assertEquals(3, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 3); assertGroup(-1, "5aa", 21, 24, mo); assertGroup(0, "5aa", 21, 24, mo); assertGroup(1, null, -1, -1, mo); assertGroup(2, "5aa", 21, 24, mo); assertEquals(2, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 4); assertGroup(-1, null, -1, -1, mo); assertEquals(0, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 5); assertGroup(-1, "5aa", 21, 24, mo); assertGroup(0, "5aa", 21, 24, mo); assertEquals(0, mo.groupCount()); } { MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 6); assertGroup(-1, "6bb", 24, 27, mo); assertGroup(0, "6bb", 24, 27, mo); assertEquals(0, mo.groupCount()); } { try { new MatchResultWithOffset(matcher, 7); fail("IndexOutOfBoundsException expected"); } catch (IndexOutOfBoundsException e) { assertEquals("No group 7", e.getMessage()); } } { try { new MatchResultWithOffset(matcher, -1); fail("IndexOutOfBoundsException expected"); } catch (IndexOutOfBoundsException e) { assertEquals("No group -1", e.getMessage()); } } }