HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java Regex as Predicate using Pattern.compile() Method

Java Regex as Predicate using Pattern.compile() Method

Learn to compile regular expression into java.util.function.Predicate. This can be useful when you want to perform some operation on matched tokens.

Convert Regex to Predicate

I have list of emails with different domain and I want to perform some operation only on email ids with domain name “example.com”.

Now use Pattern.compile().asPredicate() method to get a predicate from compiled regular expression. This predicate can be used with lambda streams to apply it on each token into stream.

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RegexPredicateExample {
	public static void main(String[] args) {
		// Compile regex as predicate
		Predicate<String> emailFilter = Pattern
										.compile("^(.+)@example.com$")
										.asPredicate();

		// Input list
		List<String> emails = Arrays.asList("alex@example.com", "bob@yahoo.com", 
							"cat@google.com", "david@example.com");

		// Apply predicate filter
		List<String> desiredEmails = emails
									.stream()
									.filter(emailFilter)
									.collect(Collectors.<String>toList());

		// Now perform desired operation
		desiredEmails.forEach(System.out::println);
	}
}

Output:

alex@example.com
david@example.com

Using Regex using Pattern.matcher()

If you want to use good old Pattern.matcher(), then use below code template.

public static void main(String[] args) 
{
	
	Pattern pattern = Pattern.compile("^(.+)@example.com$");
	
	// Input list
	List<String> emails = Arrays.asList("alex@example.com", "bob@yahoo.com", 
						"cat@google.com", "david@example.com");
	 
	for(String email : emails)
	{
	    Matcher matcher = pattern.matcher(email);
	    
	    if(matcher.matches()) 
	    {
	    	System.out.println(email);
	    }
	}
}

Output:

alex@example.com
david@example.com

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. NJ

    August 28, 2017

    I ran both the class above and figured out that the transitional approach runs quite faster compared to predicate approach .
    I want to know why and where we should predicate ?

Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 Write to File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces