Java 8 Regex as Predicate examples

Now, I want to introduce you some Java 8 Regex as Predicate examples. I make sure that you were ever in a situation in which you want to perform some operation to matched tokens. You can use Regex with Pattern.matcher() to resolve that issue but you can also do that using Regex as Predicate in Java 8.

Let’s begin.

Now, we’re considering the regular expression for validate email example

And the list of email address for testing as below

1. Convert Regex to Predicate
You can compile regex as predicate and filter that predicate like below

2. Using Regex with Lambda Expression
We also using Regex in lambda expression such as

3. Using Regex with Predicate functional interface
Predicate is a functional interface, so you can implement the Predicate body

Demo Program

Now we’re creating demo program for testing three above options

Java 8 Regex as Predicate examples

That’s all about Java 8 Regex as Predicate examples.

Predicate (Java Platform SE 8 )
Pattern (Java Platform SE 8 )

Download the complete source code, click below (296 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons