/** * Generate a predicate function that for any supplied string returns a {@link Pattern} representing the first regular expression * in the supplied comma-separated list that matches the predicate parameter in a case-insensitive manner. * * @param regexPatterns the comma-separated regular expression pattern (or literal) strings; may not be null * @return the function that performs the matching * @throws PatternSyntaxException if the string includes an invalid regular expression */ public static Function<String, Optional<Pattern>> matchedBy(String regexPatterns) { return matchedByPattern(Strings.setOfRegex(regexPatterns, Pattern.CASE_INSENSITIVE), Function.identity()); }
/** * Generate a predicate function that for any supplied parameter returns {@code true} if <i>any</i> of the regular expressions * in the supplied comma-separated list matches the predicate parameter in a case-insensitive manner. * * @param regexPatterns the comma-separated regular expression pattern (or literal) strings; may not be null * @param conversion the function that converts each predicate-supplied value to a string that can be matched against the * regular expressions; may not be null * @return the predicate function that performs the matching * @throws PatternSyntaxException if the string includes an invalid regular expression */ public static <T> Predicate<T> includes(String regexPatterns, Function<T, String> conversion) { Set<Pattern> patterns = Strings.setOfRegex(regexPatterns, Pattern.CASE_INSENSITIVE); return includedInPatterns(patterns, conversion); }
public static int isListOfRegex(Configuration config, Field field, ValidationOutput problems) { String value = config.getString(field); int errors = 0; if (value != null) { try { Strings.setOfRegex(value, Pattern.CASE_INSENSITIVE); } catch (PatternSyntaxException e) { problems.accept(field, value, "A comma-separated list of valid regular expressions is expected, but " + e.getMessage()); ++errors; } } return errors; }
@Test(expected = ParsingException.class) public void regexSplitWrongEscape() { Strings.setOfRegex("a,b\\,c\\"); }
protected void assertRegexSet(String patterns, String... matches) { Set<Pattern> regexSet = Strings.setOfRegex(patterns); assertThat(regexSet.stream() .map(Pattern::pattern) .collect(Collectors.toSet())).containsOnly((Object[])matches); }
/** * Generate a predicate function that for any supplied string returns a {@link Pattern} representing the first regular expression * in the supplied comma-separated list that matches the predicate parameter in a case-insensitive manner. * * @param regexPatterns the comma-separated regular expression pattern (or literal) strings; may not be null * @return the function that performs the matching * @throws PatternSyntaxException if the string includes an invalid regular expression */ public static Function<String, Optional<Pattern>> matchedBy(String regexPatterns) { return matchedByPattern(Strings.setOfRegex(regexPatterns, Pattern.CASE_INSENSITIVE), Function.identity()); }
/** * Generate a predicate function that for any supplied parameter returns {@code true} if <i>any</i> of the regular expressions * in the supplied comma-separated list matches the predicate parameter in a case-insensitive manner. * * @param regexPatterns the comma-separated regular expression pattern (or literal) strings; may not be null * @param conversion the function that converts each predicate-supplied value to a string that can be matched against the * regular expressions; may not be null * @return the predicate function that performs the matching * @throws PatternSyntaxException if the string includes an invalid regular expression */ public static <T> Predicate<T> includes(String regexPatterns, Function<T, String> conversion) { Set<Pattern> patterns = Strings.setOfRegex(regexPatterns, Pattern.CASE_INSENSITIVE); return includedInPatterns(patterns, conversion); }
public static int isListOfRegex(Configuration config, Field field, ValidationOutput problems) { String value = config.getString(field); int errors = 0; if (value != null) { try { Strings.setOfRegex(value, Pattern.CASE_INSENSITIVE); } catch (PatternSyntaxException e) { problems.accept(field, value, "A comma-separated list of valid regular expressions is expected, but " + e.getMessage()); ++errors; } } return errors; }