/** * Helper function to check if a string value matches the provided pattern. Note, if matching * many values against the same pattern, then it is much more efficient to use * {@link #compile(String)} to get an instance of a matcher that can be reused. * * @param pattern * Pattern to use for creating the matcher instance. * @param value * Value to check against the pattern. * @return * True if the pattern matches the value. */ static boolean matches(String pattern, String value) { return compile(pattern).matches(value); } }
/** Create a new instance. */ Regex(String k, String v, boolean ignoreCase, String name) { this.k = Preconditions.checkNotNull(k, "k"); this.v = Preconditions.checkNotNull(v, "v"); if (ignoreCase) { this.pattern = PatternMatcher.compile("^" + v).ignoreCase(); } else { this.pattern = PatternMatcher.compile("^" + v); } this.name = Preconditions.checkNotNull(name, "name"); }
/** Create a new instance. */ OrMatcher(Matcher... matchers) { this.matchers = matchers; int min = Integer.MAX_VALUE; for (PatternMatcher matcher : matchers) { min = Math.min(min, matcher.minLength()); } this.minLength = min; }
@Test public void prefix() { Assertions.assertEquals("abc", PatternMatcher.compile("^abc").prefix()); Assertions.assertNull(PatternMatcher.compile("abc").prefix()); Assertions.assertEquals("abc", PatternMatcher.compile("^(abc)").prefix()); Assertions.assertEquals("abc", PatternMatcher.compile("^(abc|abcdef)").prefix()); Assertions.assertEquals("abc", PatternMatcher.compile("^[a][b][c]").prefix()); Assertions.assertEquals("abc", PatternMatcher.compile("^[a][b][c]+").prefix()); }
private void testBadExpression(String regex, String message) { //Pattern.compile(regex); intercept(IllegalArgumentException.class, message, () -> PatternMatcher.compile(regex)); }
@Override protected void testRE(String regex, String value) { // Java regex has inconsistent behavior for POSIX character classes and the literal version // of the same character class. For now we skip regex that use POSIX classes. // https://bugs.openjdk.java.net/browse/JDK-8214245 if (!regex.contains("\\p{") && !regex.contains("\\P{") && !regex.contains("[^")) { int flags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE; Pattern pattern = Pattern.compile("^.*(" + regex + ")", flags); PatternMatcher matcher = PatternMatcher.compile(regex).ignoreCase(); if (pattern.matcher(value).find()) { Assertions.assertTrue(matcher.matches(value), regex + " should match " + value); } else { Assertions.assertFalse(matcher.matches(value), regex + " shouldn't match " + value); } // Check pattern can be recreated from toString PatternMatcher actual = PatternMatcher.compile(matcher.toString()); Assertions.assertEquals(matcher, actual); } } }
@Override public boolean matches(String str) { return matcher.matches(str); }
@Test public void isStartAnchored() { Assertions.assertTrue(PatternMatcher.compile("^abc").isStartAnchored()); Assertions.assertTrue(PatternMatcher.compile("^[a-z]").isStartAnchored()); Assertions.assertTrue(PatternMatcher.compile("(^a|^b)").isStartAnchored()); Assertions.assertFalse(PatternMatcher.compile("(^a|b)").isStartAnchored()); Assertions.assertFalse(PatternMatcher.compile("abc").isStartAnchored()); }
@Test public void isEndAnchored() { Assertions.assertTrue(PatternMatcher.compile("abc$").isEndAnchored()); Assertions.assertTrue(PatternMatcher.compile("[a-z]$").isEndAnchored()); Assertions.assertTrue(PatternMatcher.compile("(a|b)$").isEndAnchored()); Assertions.assertFalse(PatternMatcher.compile("(a$|b)").isEndAnchored()); Assertions.assertFalse(PatternMatcher.compile("abc").isEndAnchored()); }
@Test public void alwaysMatches() { Assertions.assertTrue(PatternMatcher.compile(".*").alwaysMatches()); Assertions.assertFalse(PatternMatcher.compile(".?").alwaysMatches()); Assertions.assertFalse(PatternMatcher.compile("$.").alwaysMatches()); }
@Test public void neverMatches() { Assertions.assertFalse(PatternMatcher.compile(".*").neverMatches()); Assertions.assertFalse(PatternMatcher.compile(".?").neverMatches()); Assertions.assertTrue(PatternMatcher.compile("$.").neverMatches()); }
private void testUnsupported(String regex, String message) { intercept(UnsupportedOperationException.class, message, () -> PatternMatcher.compile(regex)); }
@Override public boolean matches(String str) { return matcher.matches(str); }
@Test public void unknownQuantifier() { Assertions.assertThrows(IllegalArgumentException.class, () -> PatternMatcher.compile(".+*")); }
/** * Helper function to check if a string value matches the provided pattern. Note, if matching * many values against the same pattern, then it is much more efficient to use * {@link #compile(String)} to get an instance of a matcher that can be reused. * * @param pattern * Pattern to use for creating the matcher instance. * @param value * Value to check against the pattern. * @return * True if the pattern matches the value. */ static boolean matches(String pattern, String value) { return compile(pattern).matches(value); } }
@Override public boolean matches(Map<String, String> tags) { String s = tags.get(k); return s != null && pattern.matches(s); }
/** Create a new instance. */ OrMatcher(Matcher... matchers) { this.matchers = matchers; int min = Integer.MAX_VALUE; for (PatternMatcher matcher : matchers) { min = Math.min(min, matcher.minLength()); } this.minLength = min; }
@Test public void unclosedNamedCapturingGroup() { Assertions.assertThrows(IllegalArgumentException.class, () -> PatternMatcher.compile("(?<foo)")); }
@Override protected void testRE(String regex, String value) { Pattern pattern = Pattern.compile("^.*(" + regex + ")", Pattern.DOTALL); PatternMatcher matcher = PatternMatcher.compile(regex); if (pattern.matcher(value).find()) { Assertions.assertTrue(matcher.matches(value), regex + " should match " + value); } else { Assertions.assertFalse(matcher.matches(value), regex + " shouldn't match " + value); } // Check pattern can be recreated from toString PatternMatcher actual = PatternMatcher.compile(matcher.toString()); Assertions.assertEquals(matcher, actual); } }
@Test public void unbalancedClosingParen() { Assertions.assertThrows(IllegalArgumentException.class, () -> PatternMatcher.compile("(abc))")); }