@Test public void testEqualsWithSamePatternButDiffFlagsGetsFalse() { Pattern p1 = Pattern.compile("(a)(b)(?:c)(?<named>x)"); Pattern p2 = Pattern.compile("(a)(b)(?:c)(?<named>x)", java.util.regex.Pattern.CASE_INSENSITIVE); assertFalse(p1.equals(p2)); }
@Test public void testStandardPatternGetsOrigWithoutNamed() { final String ORIG_PATT = "(a)(b)(?:c)(?<named>x)"; final String PATT_W_NO_NAMED_GRPS = "(a)(b)(?:c)(x)"; Pattern p = Pattern.compile(ORIG_PATT); assertEquals(PATT_W_NO_NAMED_GRPS, p.standardPattern()); }
@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 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")); }
public void testInvalidGroup(String text, String regexp, int group) { Pattern p = Pattern.compile(regexp, options); Matcher m = p.matcher(utf8Slice(text)); m.find(); m.group(group); fail(); // supposed to have exception by now }
@Test public void testCompileWithUnknownBackref() { thrown.expect(PatternSyntaxException.class); thrown.expectMessage( "unknown group name near index 11\n" + "(xyz)abc\\k<bar>\n" + " ^"); Pattern.compile("(?<foo>xyz)abc\\k<bar>"); }
public void testGroup(String text, String regexp, String[] output) { // RE2 Pattern p = Pattern.compile(regexp, options); Matcher matchString = p.matcher(utf8Slice(text)); assertEquals(true, matchString.find()); assertEquals(utf8Slice(output[0]), matchString.group()); for (int i = 0; i < output.length; i++) { assertEquals(output[i] == null ? null : utf8Slice(output[i]), matchString.group(i)); } assertEquals(output.length - 1, matchString.groupCount()); // JDK java.util.regex.Pattern pj = java.util.regex.Pattern.compile(regexp); java.util.regex.Matcher matchStringj = pj.matcher(text); // java.util.regex.Matcher matchBytes = // p.matcher(text.getBytes(Charsets.UTF_8)); assertEquals(true, matchStringj.find()); // assertEquals(true, matchBytes.find()); assertEquals(output[0], matchStringj.group()); // assertEquals(output[0], matchBytes.group()); for (int i = 0; i < output.length; i++) { assertEquals(output[i], matchStringj.group(i)); // assertEquals(output[i], matchBytes.group(i)); } }
@Test public void testGroupInfoMapHasCorrectPosAndGroupIndex() { final String PATT = "(foo)(?<X>a)(?<Y>b)(bar)(?<Z>c)(?<Z>d)"; // two groups named "Z" Pattern p = Pattern.compile(PATT); Map<String, List<GroupInfo>> map = p.groupInfo(); assertNotNull(map); GroupInfo[] inf = (GroupInfo[]) map.get("X").toArray(new GroupInfo[0]); assertEquals(1, inf.length); assertEquals(PATT.indexOf("(?<X>"), inf[0].pos()); assertEquals(1, inf[0].groupIndex()); GroupInfo[] inf2 = (GroupInfo[]) map.get("Y").toArray(new GroupInfo[0]); assertEquals(1, inf2.length); assertEquals(PATT.indexOf("(?<Y>"), inf2[0].pos()); assertEquals(2, inf2[0].groupIndex()); // test both Z groups GroupInfo[] inf3 = (GroupInfo[]) map.get("Z").toArray(new GroupInfo[0]); assertEquals(2, inf3.length); int posZ = PATT.indexOf("(?<Z>"); assertEquals(posZ, inf3[0].pos()); assertEquals(4, inf3[0].groupIndex()); assertEquals(PATT.indexOf("(?<Z>", posZ + 1), inf3[1].pos()); assertEquals(5, inf3[1].groupIndex()); }
@Test public void testCompileRegexWithFlags() { final String PATT = "(?<name>abc) # comment 1"; int flags = java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.COMMENTS; Pattern p = Pattern.compile(PATT, flags); assertEquals(PATT, p.namedPattern()); assertEquals(flags, p.flags()); }
@Test public void testSplitGetsArrayOfTextAroundMatches() { Pattern p = Pattern.compile("(a)(b)(?:c)(?<named>x)"); assertArrayEquals(new String[] {"foo ", " bar "}, p.split("foo abcx bar abcx")); // when the limit is specified, the last element contains // the remainder of the string assertArrayEquals(new String[] {"foo ", " bar abcx"}, p.split("foo abcx bar abcx", 2)); }
@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 testCompileBackrefTakesFirstClosingAngleBracket() { String GROUP_NAME = "foo bar >"; Pattern p = Pattern.compile("(?<foo>xyz)(?<" + GROUP_NAME + ">\\d+)abc\\k<" + GROUP_NAME + ">"); // The first closing bracket encountered is used. The second becomes a literal, // so we check for it in the standard pattern (two actually). assertEquals("(xyz)(>\\d+)abc\\2>", p.standardPattern()); }
@Test public void testTakesPatternWithEscapedEscape() { // it looks like an escaped parenthesis, but the escape char is // itself escaped and is thus a literal final String PATT = "\\\\(?<name>abc)"; Pattern p = Pattern.compile(PATT); assertEquals("\\\\(abc)", p.standardPattern()); }
@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 testGroupNames() { final String PATT = "(foo)(?<X>a)(?<Y>b)(?<Z>c)(bar)"; Pattern p = Pattern.compile(PATT); assertNotNull(p.groupNames()); assertEquals(3, p.groupNames().size()); assertEquals("X", p.groupNames().get(0)); assertEquals("Y", p.groupNames().get(1)); assertEquals("Z", p.groupNames().get(2)); }
public void testMatcherMatches(String regexp, String match) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(regexp); assertTrue( "JDK Pattern with regexp: " + regexp + " doesn't match: " + match, p.matcher(match).matches()); Pattern pr = Pattern.compile(regexp, options); assertTrue( "Pattern with regexp: " + regexp + " doesn't match: " + match, pr.matcher(utf8Slice(match)).matches()); }
public void testMatcherNotMatches(String regexp, String nonMatch) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(regexp); assertFalse( "JDK Pattern with regexp: " + regexp + " matches: " + nonMatch, p.matcher(nonMatch).matches()); Pattern pr = Pattern.compile(regexp, options); assertFalse( "Pattern with regexp: " + regexp + " matches: " + nonMatch, pr.matcher(utf8Slice(nonMatch)).matches()); }
@Test public void testGroupInfoMapHasNamesAsKeys() { final String PATT = "(foo)(?<X>a)(?<Y>b)(bar)(?<Z>c)(?<Z>d)"; // two groups named "Z" Pattern p = Pattern.compile(PATT); Map<String, List<GroupInfo>> map = p.groupInfo(); assertNotNull(map); assertEquals(3, map.size()); assertTrue(map.containsKey("X")); assertTrue(map.containsKey("Y")); assertTrue(map.containsKey("Z")); }
// Tests that both RE2 and JDK's Matchers do the same replaceFist. public void testReplaceFirst(String orig, String regex, String repl, String actual) { Pattern p = Pattern.compile(regex, options); Matcher m = p.matcher(utf8Slice(orig)); String replaced = m.replaceFirst(utf8Slice(repl)).toStringUtf8(); assertEquals(actual, replaced); // JDK's java.util.regex.Pattern pj = java.util.regex.Pattern.compile(regex); java.util.regex.Matcher mj = pj.matcher(orig); replaced = mj.replaceFirst(repl); assertEquals(actual, replaced); }
// Tests that both RE2 and JDK's Patterns/Matchers give the same groupCount. public void testGroupCount(String pattern, int count) { // RE2 Pattern p = Pattern.compile(pattern, options); Matcher m = p.matcher(utf8Slice("x")); assertEquals(count, p.groupCount()); assertEquals(count, m.groupCount()); // JDK java.util.regex.Pattern pj = java.util.regex.Pattern.compile(pattern); java.util.regex.Matcher mj = pj.matcher("x"); // java.util.regex.Pattern doesn't have group count in JDK. assertEquals(count, mj.groupCount()); }
public void testFindNoMatch(String text, String regexp, int start) { // RE2 Pattern p = Pattern.compile(regexp, options); Matcher matchString = p.matcher(utf8Slice(text)); // RE2Matcher matchBytes = p.matcher(text.getBytes(Charsets.UTF_8)); assertFalse(matchString.find(start)); // assertFalse(matchBytes.find(start)); // JDK java.util.regex.Pattern pj = java.util.regex.Pattern.compile(regexp); java.util.regex.Matcher matchStringj = pj.matcher(text); assertFalse(matchStringj.find(start)); }
public void testFind(String text, String regexp, int start, String output) { // RE2 Pattern p = Pattern.compile(regexp, options); Matcher matchString = p.matcher(utf8Slice(text)); assertTrue(matchString.find(start)); assertEquals(utf8Slice(output), matchString.group()); assertTrue(p.find(utf8Slice(text))); // JDK java.util.regex.Pattern pj = java.util.regex.Pattern.compile(regexp); java.util.regex.Matcher matchStringj = pj.matcher(text); assertTrue(matchStringj.find(start)); assertEquals(output, matchStringj.group()); }
@Test public void testIndexOfNamedGroupAfterAnotherNamedGroup() { Pattern p = Pattern.compile("(a)(?<foo>)(?:c)(?<named>x)"); assertEquals(2, p.indexOf("named")); }
@Test public void testIndexOfNamedGroupAfterUnnamedAndNoncaptureGroups() { Pattern p = Pattern.compile("(a)(b)(?:c)(?<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 testIndexOfNameWithUnicodeChars() { Pattern p = Pattern.compile("(?<gefräßig>x)"); assertEquals(0, p.indexOf("gefräßig")); }
@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 testIndexOfAcceptsNameWithClosingAngleBracket() { Pattern p = Pattern.compile("(?<foo bar > should not grab this bracket> x)"); assertEquals(0, p.indexOf("foo bar ")); }
@Test public void testCompileBackrefAcceptsNameWithUnicodeChars() { String GROUP_NAME = "gefräßig"; Pattern p = Pattern.compile("(?<foo>xyz)(?<" + GROUP_NAME + ">\\d+)abc\\k<" + GROUP_NAME + ">"); assertEquals("(xyz)(\\d+)abc\\2", p.standardPattern()); }
@Test public void testCompileBackrefAcceptsNameWithNewLines() { String GROUP_NAME = "Lorem ipsum dolor sit amet,\n consectetur adipisicing elit"; Pattern p = Pattern.compile("(?<foo>xyz)(?<" + GROUP_NAME + ">\\d+)abc\\k<" + GROUP_NAME + ">"); assertEquals("(xyz)(\\d+)abc\\2", p.standardPattern()); }