O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filtering a list of tokens using the asPredicate() method

As noted in the preceding table, the asPredicate() method creates a predicate that can be used to match an input string. Let's look at an example code listing to understand this method better:

package example.regex; import java.util.List; import java.util.stream.*; import java.util.regex.*; public class AsPredicateExample {   public static void main(String[] args)   {     final String[] monthsArr =  {"10", "0", "05", "09", "12", "15", "00", "-1", "100"};     final Pattern validMonthPattern =  Pattern.compile("^(?:0?[1-9]|1[00-2])$");     List<String> filteredMonths = Stream.of(monthsArr)       .filter(validMonthPattern.asPredicate())       .collect(Collectors.toList());  System.out.println(filteredMonths); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required