Java Code Examples for java.util.regex.Pattern#asMatchPredicate()

The following examples show how to use java.util.regex.Pattern#asMatchPredicate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewApi.java    From Java-11-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
private static void asMatchPredicate(){
    Pattern pattern =  Pattern.compile("^a.*z$");
    Predicate<String> predicate = pattern.asMatchPredicate();
    System.out.println(predicate.test("abbbbz"));  // true
    System.out.println(predicate.test("babbbz"));  // false
    System.out.println(predicate.test("abbbbx"));  // false
}
 
Example 2
Source File: PatternJava11UnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesPattern() {
    List<String> namesToValidate = Arrays.asList("Fabio Silva", "Fabio Luis Silva");
    Pattern firstLastNamePreCompiledPattern = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");

    Predicate<String> patternAsMatchPredicate = firstLastNamePreCompiledPattern.asMatchPredicate();
    List<String> validatedNames = namesToValidate.stream()
            .filter(patternAsMatchPredicate)
            .collect(Collectors.toList());

    assertTrue(validatedNames.contains("Fabio Silva"));
    assertFalse(validatedNames.contains("Fabio Luis Silva"));
}