@Test public void testIndexOfNamedGroupContainingSpecialConstruct() { Pattern p = Pattern.compile( "\\d{2}/\\d{2}/\\d{4}: EXCEPTION - (?<exception>(?s)(.+(?:Exception|Error)[^\\n]+(?:\\s++at [^\\n]+)++)(?:\\s*\\.{3}[^\\n]++)?\\s*)\\n"); assertEquals(0, p.indexOf("exception")); }
@Test public void testIndexOfWithInvalidPositiveInstanceIndex() { Pattern p = Pattern.compile("(a)(?<named>x)(b)(?:c)(?<named>y)"); thrown.expect(IndexOutOfBoundsException.class); thrown.expectMessage("Index: 10000000, Size: 2"); assertEquals(-1, p.indexOf("named", 10000000)); }
@Test public void testIndexOfNamedGroupAfterNonEscapedParenAfterEscapedOpenBracket() { // since the open-bracket is escaped, it doesn't create a character class, // so the parentheses inside the "foo" group is a capturing group (that // currently captures nothing but still valid regex and thus counted) Pattern p = Pattern.compile("(a)(?<foo>\\[()])(?:c)(?<named>x)"); assertEquals(3, p.indexOf("named")); }
@Test public void testIndexOfWithInvalidNegativeInstanceIndex() { Pattern p = Pattern.compile("(a)(?<named>x)(b)(?:c)(?<named>y)"); // Negative index causes ArrayIndexOutOfBoundsException (which // is a subclass of IndexOutOfBoundsException) thrown.expect(ArrayIndexOutOfBoundsException.class); thrown.expectMessage("-100"); assertEquals(-1, p.indexOf("named", -100)); }
@Test public void testIndexOfWithMultipleGroupsWithSameName() { Pattern p = Pattern.compile("(a)(?<named>x)(b)(?:c)(?<named>y)"); assertEquals(3, p.indexOf("named", 1)); }
@Test public void testIndexOfNamedGroupAtMiddle() { Pattern p = Pattern.compile("(a)(?<named>x)(b)(?:c)"); assertEquals(1, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterEscapedParen() { Pattern p = Pattern.compile("\\(a\\)\\((b)\\)(?:c)(?<named>x)"); assertEquals(1, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterUnnamedAndNoncaptureGroups() { Pattern p = Pattern.compile("(a)(b)(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNameWithUnicodeChars() { Pattern p = Pattern.compile("(?<gefräßig>x)"); assertEquals(0, p.indexOf("gefräßig")); }
@Test public void testIndexOfAcceptsNameWithClosingAngleBracket() { Pattern p = Pattern.compile("(?<foo bar > should not grab this bracket> x)"); assertEquals(0, p.indexOf("foo bar ")); }
@Test public void testIndexOfAcceptsClassName() { Pattern p = Pattern.compile("(?<com.example.foo>x)"); assertEquals(0, p.indexOf("com.example.foo")); }
@Test public void testIndexOfNamedGroupAfterNonEscapedParenInCharClassWithEscapedCloseBracket() { Pattern p = Pattern.compile("(a)(?<foo>[\\]()])(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterSlashedCloseParenInCharacterClass() { // double-slashes in a character class are literal slashes, not escapes Pattern p = Pattern.compile("(a)(?<foo>[(\\\\)])(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterEscapedCloseParenInCharacterClass() { Pattern p = Pattern.compile("(a)(?<foo>[(\\)])(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupBeforeSpecialConstruct1() { Pattern p = Pattern.compile("(?<named>x)(?idsumx-idsumx)(?=b)(?!x)"); assertEquals(0, p.indexOf("named")); }
@Test public void testIndexOfNotFound() { Pattern p = Pattern.compile("(a)(b)(?:c)(?<named>x)"); assertEquals(-1, p.indexOf("dummy")); }
@Test public void testIndexOfWithPositiveLookbehindAtBeginning() { Pattern p = Pattern.compile("(?<=a)(b)(c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfAcceptsNameWithSpacesAndPunctuation() { Pattern p = Pattern.compile("(?< Lorem ipsum dolor sit amet, consectetur adipisicing elit>x)"); assertEquals(0, p.indexOf(" Lorem ipsum dolor sit amet, consectetur adipisicing elit")); }
@Test public void testIndexOfWithNegativeLookahead() { Pattern p = Pattern.compile("(a)(b)(?!c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfAcceptsNameWithNewLines() { Pattern p = Pattern.compile("(?<Lorem ipsum dolor sit amet,\n consectetur adipisicing elit>x)"); assertEquals(0, p.indexOf("Lorem ipsum dolor sit amet,\n consectetur adipisicing elit")); }
@Test public void testIndexOfWithFlagsAndExtraNoCapture() { Pattern p = Pattern.compile("(a)(b)(?idsumx:Z)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNamedGroup() { Pattern p = Pattern.compile("(?<named>x)"); assertEquals(0, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAtBeginning() { Pattern p = Pattern.compile("(?<named>x)(a)(b)(?:c)"); assertEquals(0, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterAnotherNamedGroup() { Pattern p = Pattern.compile("(a)(?<foo>)(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNestedNamedGroup() { Pattern p = Pattern.compile("(a)(?<foo>b)(?:c)(?<bar>d(?<named>x))"); assertEquals(3, p.indexOf("named")); }