Spring EL regular expression example

Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples,

@Value("#{'100' matches '\\d+' }")  
private boolean isDigit;

It test whether ‘100‘ is a valid digit via regular expression ‘\\d+‘.

Spring EL in Annotation

See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL pretty flexible and powerful.
Below example should be self-explanatory.

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

    // email regular expression
    String emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)" +
                                       "*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    // if this is a digit?
   @Value("#{'100' matches '\\d+' }")
    private boolean validDigit;

    // if this is a digit + ternary operator
    @Value("#{ ('100' matches '\\d+') == true ? " +
                   "'yes this is digit' : 'No this is not a digit'  }")
    private String msg;

    // if this emailBean.emailAddress contains a valid email address?
    @Value("#{emailBean.emailAddress matches customerBean.emailRegEx}")
     private boolean validEmail;

     //getter and setter methods, and constructor
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("emailBean")
public class Email {    
    @Value("nospam@abc.com")    
    String emailAddress;    
    //...
}

Output

Customer [isDigit=true, msg=yes this is digit, isValidEmail=true]

Spring EL in XML

See equivalent version in bean definition XML file.

<beans xmlns="http://www.springframework.org/schema/beans"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xsi:schemaLocation="http://www.springframework.org/schema/beans    
                                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
     <bean id="customerBean" class="com.mkyong.core.Customer">   
         <property name="validDigit" value="#{'100' matches '\d+' }" />  
        <property name="msg" value="#{ ('100' matches '\d+') == true ? 'yes this is digit' : 'No this is not a digit' }" />  
        <property name="validEmail" value="#{emailBean.emailAddress matches '^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$' }" />   
    </bean> 
    <bean id="emailBean" class="com.mkyong.core.Email">  
        <property name="emailAddress" value="nospam@abc.com" /> 
    </bean>
</beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan阅读 5,445评论 7 56
  • 我很久以前就听说写作的好处。而且现在自媒体创业很火,知识变现,内容创业看看微信上好像满天飞的知识变现百万富翁。...
    诸葛妙计阅读 291评论 0 2
  • 她坐在彩虹下 伤怀于烤干的土痂 扑在他怀里哭诉 “不见了,呜~专一的雨洼。” 他轻轻地环抱她 抚弄着温柔的头发 看...
    露枫阅读 263评论 0 0
  • 老船木越来越被人熟知,但是真正了解的人不多,到底要不要选择老船木呢?为什么会有越来越多的人选择它?我们的家合适用老...
    ff695ff9698f阅读 464评论 0 0