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 EL regular expression example的更多相关文章

  1. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  2. Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...

  3. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...

  4. Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

    Spring  EL 一:在Spring xml 配置文件中运用   Spring EL Spring EL 采用 #{Sp Expression  Language} 即 #{spring表达式} ...

  5. Spring学习(十三)-----Spring 表达式语言(Spring EL)

    本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...

  6. Spring EL hello world实例

    Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...

  7. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  8. spring 使用Spring表达式(Spring EL)

    Spring还提供了更灵活的注入方式,那就是Spring表达式,实际上Spring EL远比以上注入方式强大,我们需要学习它.Spring EL拥有很多功能. 使用Bean的id来引用Bean. •调 ...

  9. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. ListView(1)几个重要属性,关闭滚动到顶部,底部的动画,item之间的分割线,背景等

    见表: android:stackFromBottom="true" 设置该属性之后你做好的列表就会显示你列表的最下面,值为true和false android:transcrip ...

  2. BZOJ 1297 迷路(矩阵)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1297 题意:给出一个带权有向图,权值为1-9,顶点个数最多为10.从1出发恰好在T时刻到 ...

  3. JPA和Hibernate的区别

    JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb3规范的一部分. Hibernate,当今很流行的ORM框架,是JPA的一个实现,但是其功能是JPA的 ...

  4. 1128. Partition into Groups(图着色bfs)

    1128 写的dfs貌似不太对 bfs重写 用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 ...

  5. UVa 11054 Wine trading in Gergovia【贪心】

    题意:给出n个等距离的村庄,每个村庄要么买酒,要么卖酒,买酒和卖酒的总量相等, 把k个单位的酒从一个村庄运送到相邻的村庄,需要耗费k个单位劳动力,问怎样运送酒使得耗费的劳动力最少 买     卖    ...

  6. WinForm 禁止调整大小、禁止最大化窗口

    这个设置代码必须添加到*.designer.cs中,就是自动隐藏的那部分: #region Windows Form Designer generated code 一般窗体设置的代码会生成到最后面, ...

  7. ZOJ 2587 Unique Attack (最小割唯一性)

    题意 判断一个无向图的割是否唯一 思路 错误思路:一开始想的是判断割边是否都是关键割边,那既然割边两端点能连通S.T点的边是关键边,那么只要遇到有某个边两端点不连通S or T则这条边就不是关键割边( ...

  8. 【大数加法】POJ-1503、NYOJ-103

    1503:Integer Inquiry 总时间限制:  1000ms 内存限制:  65536kB 描述 One of the first users of BIT's new supercompu ...

  9. Wireshark和TcpDump抓包分析心得

    Wireshark和 TcpDump抓包分析心得  1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...

  10. Oracle表操作 (未完待续)

    1. Oracle 中将一个表中数据导入到另外一个表的方法 insert into scd_data_201007 select * from analog_data_201007 ; 2. 纵表转换 ...