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)); } }
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 }
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()); }
// 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); }
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)); }
// 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 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()); }
public void testReplaceAllRE2J(String orig, String regex, String repl, String actual) { Pattern p = Pattern.compile(regex, options); Matcher m = p.matcher(utf8Slice(orig)); String replaced = m.replaceAll(utf8Slice(repl)).toStringUtf8(); assertEquals(actual, replaced); }
private Map<String, j86.com.sun.jdi.connect.Connector.Argument> parseConnectorArgs( Connector connector, String argString) { Map<String, j86.com.sun.jdi.connect.Connector.Argument> arguments = connector.defaultArguments(); /* * We are parsing strings of the form: * name1=value1,[name2=value2,...] * However, the value1...valuen substrings may contain * embedded comma(s), so make provision for quoting inside * the value substrings. (Bug ID 4285874) */ String regexPattern = "(quote=[^,]+,)|" + // special case for quote=., "(\\w+=)" + // name= "(((\"[^\"]*\")|" + // ( "l , ue" "('[^']*')|" + // 'l , ue' "([^,'\"]+))+,)"; // v a l u e )+ , Pattern p = Pattern.compile(regexPattern); Matcher m = p.matcher(argString); while (m.find()) { int startPosition = m.start(); int endPosition = m.end(); if (startPosition > 0) { /* * It is an error if parsing skips over any part of argString. */ throw new IllegalArgumentException( MessageOutput.format("Illegal connector argument", argString)); } String token = argString.substring(startPosition, endPosition); int index = token.indexOf('='); String name = token.substring(0, index); String value = token.substring(index + 1, token.length() - 1); // Remove comma delimiter /* * for values enclosed in quotes (single and/or double quotes) * strip off enclosing quote chars * needed for quote enclosed delimited substrings */ if (name.equals("options")) { StringBuilder sb = new StringBuilder(); for (String s : splitStringAtNonEnclosedWhiteSpace(value)) { while (isEnclosed(s, "\"") || isEnclosed(s, "'")) { s = s.substring(1, s.length() - 1); } sb.append(s); sb.append(" "); } value = sb.toString(); } Connector.Argument argument = arguments.get(name); if (argument == null) { throw new IllegalArgumentException( MessageOutput.format( "Argument is not defined for connector:", new Object[] {name, connector.name()})); } argument.setValue(value); argString = argString.substring(endPosition); // Remove what was just parsed... m = p.matcher(argString); // and parse again on what is left. } if ((!argString.equals(",")) && (argString.length() > 0)) { /* * It is an error if any part of argString is left over, * unless it was empty to begin with. */ throw new IllegalArgumentException( MessageOutput.format("Illegal connector argument", argString)); } return arguments; }