How do I do pattern matching using regular expression in Spring EL?

In this Spring Expression Language example we are going to learn how to use regular expression or regex to check if a text matches a certain pattern. Spring EL support regular expression using the matches operator.

The matches operator will check if the value has a pattern defined by the regex string and it returns the evaluation result as a boolean value true if the text matches the regex or false if otherwise.

For example we can use the matches operator to check is a the given email address is a valid email address. As can be seen in the following example:

<property name="emailValid" 
          value="#{ matches '^[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$'}"/>

This configuration will evaluate the property to check if the email pattern matches with the given regular expression. If matches then the emailValid property will be set to true otherwise it will be false.

Let’s see the complete example. Here are the spring configuration file, the User bean and a simple class for running the configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="user" class="org.kodejava.example.spring.model.User">
        <constructor-arg name="email" value="[email protected]"/>
        <property name="emailValid"
                  value="#{ matches '^[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$'}"/>

The User bean is a simple pojo with two properties, a string email property and a boolean validEmail property.

package org.kodejava.example.spring.model;

public class User {
    private String email;
    private boolean emailValid;

    public User() {

    public User(String email) { = email;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public boolean isEmailValid() {
        return emailValid;

    public void setEmailValid(boolean emailValid) {
        this.emailValid = emailValid;

And finally the application class.

package org.kodejava.example.spring;

import org.kodejava.example.spring.model.User;
import org.springframework.context.ApplicationContext;

public class SpELRegexExample {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spel-regex.xml");

        User user = (User) context.getBean("user");
        System.out.println("user.getEmail()     = " + user.getEmail());
        System.out.println("user.isEmailValid() = " + user.isEmailValid());

When we run the code we will obtain the following result:

INFO: Loading XML bean definitions from class path resource [spel-regex.xml]
user.getEmail()     = [email protected]
user.isEmailValid() = true
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.
Wayan Saryada

Leave a Reply