正则表达式是一个字符串,这个字符串可以来描述或者匹配一系列符合某个语法规则的字符串。广泛用于表单验证中,比如匹配ip地址,电子邮箱等。
正则表达式本身具有短小精悍的特点,使用它可以避免编写很多逻辑复杂的代码,以完成某个数据合法性的检测。最近的工作中遇到了很多这种类似的校验,比如时间格式符合”yyyy-MM-dd hh:mm:ss”规格,路径中不能包含特殊字符和中文字符,IP地址校验等等
收集了一些常用的正则表达式,供日常工作中用到时查询。不过还是有空学习一下正则表达式的语法,在遇到新的数据格式需要校验的时候,可以自己动手把表达式写出来。
首先是最近工作中用到的正则表达式:
检测时间格式 yyyy-MM-dd hh:mm:ss 的表达式:
^(((20[0-3][0-9]-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|(20[0-3][0-9]-(0[2469]|11)-(0[1-9]|[12][0-9]|30))) (20|21|22|23|[0-1][0-9]):[0-5][0-9]:[0-5][0-9])$
检测合法目录格式:合法的盘符,不包含特殊字符和中文路径
[a-zA-Z]:[/\\\\][\\.\\w\\-_/\\\\]+
检测合法的IP地址:
^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+___FCKpd___0quot; //email地址
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?___FCKpd___0quot; //url
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$) //email地址
package com.ygj.util;
import java.util.*;
import org.apache.oro.text.regex.*;
/**
* 类简介: 使用正则表达式验证数据或提取数据,类中的方法全为静态的
* 主要方法:1. isHardRegexpValidate(String source, String regexp)
区分大小写敏感的正规表达式批配 * 2. isSoftRegexpValidate(String source, String regexp)
* 不区分大小写的正规表达式批配
* 3. getHardRegexpMatchResult(String source, String regexp)
* 返回许要的批配结果集(大小写敏感的正规表达式批配)
* 4. getSoftRegexpMatchResult(String source, String regexp)
* 返回许要的批配结果集(不区分大小写的正规表达式批配)
* 5 getHardRegexpArray(String source, String regexp)
* 返回许要的批配结果集(大小写敏感的正规表达式批配)
* 6. getSoftRegexpMatchResult(String source, String regexp)
* 返回许要的批配结果集(不区分大小写的正规表达式批配)
* 7. getBetweenSeparatorStr(final String originStr,final char leftSeparator,final char rightSeparator)
* 得到指定分隔符中间的字符串的集合
*
* @mail wuzhi2000@hotmail.com
* @author ygj
*
*/
public final class Regexp
{
/** 保放有四组对应分隔符 */
static final Set SEPARATOR_SET=new TreeSet();
{
SEPARATOR_SET.add("(");
SEPARATOR_SET.add(")");
SEPARATOR_SET.add("[");
SEPARATOR_SET.add("]");
SEPARATOR_SET.add("{");
SEPARATOR_SET.add("}");
SEPARATOR_SET.add("<");
SEPARATOR_SET.add(">");
}
/** 存放各种正规表达式(以key->value的形式) */
public static HashMap regexpHash = new HashMap();
/** 存放各种正规表达式(以key->value的形式) */
public static List matchingResultList = new ArrayList();
private Regexp()
{
}
/**
* 返回 Regexp 实例
* @return
*/
public static Regexp getInstance()
{
return new Regexp();
}
/**
* 匹配图象
*
* 格式: /相对路径/文件名.后缀 (后缀为gif,dmp,png)
*
* 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp
*
* 不匹配: c:/admins4512.gif
*
*/
public static final String icon_regexp = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg)$|^\\w{1,}\\.(gif|dmp|png|jpg)$";
/**
* 匹配email地址
*
* 格式: XXX@XXX.XXX.XX
*
* 匹配 : foo@bar.com 或 foobar@foobar.com.au
*
* 不匹配: foo@bar 或 $$$@bar.com
*
*/
public static final String email_regexp = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";
/**
* 匹配匹配并提取url
*
* 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX
*
* 匹配 : http://www.suncer.com 或news://www
*
* 提取(MatchResult matchResult=matcher.getMatch()):
* matchResult.group(0)= http://www.suncer.com:8080/index.html?login=true
* matchResult.group(1) = http
* matchResult.group(2) = www.suncer.com
* matchResult.group(3) = :8080
* matchResult.group(4) = /index.html?login=true
*
* 不匹配: c:\window
*
*/
public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";
/**
* 匹配并提取http
*
* 格式: http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX 或 ftp://XXX.XXX.XXX 或 https://XXX
*
* 匹配 : http://www.suncer.com:8080/index.html?login=true
*
* 提取(MatchResult matchResult=matcher.getMatch()):
* matchResult.group(0)= http://www.suncer.com:8080/index.html?login=true
* matchResult.group(1) = http
* matchResult.group(2) = www.suncer.com
* matchResult.group(3) = :8080
* matchResult.group(4) = /index.html?login=true
*
* 不匹配: news://www
*
*/
public static final String http_regexp = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";
/**
* 匹配日期
*
* 格式(首位不为0): XXXX-XX-XX 或 XXXX XX XX 或 XXXX-X-X
*
* 范围:1900--2099
*
* 匹配 : 2005-04-04
*
* 不匹配: 01-01-01
*
*/
public static final String date_regexp = "^((((19){1}|(20){1})d{2})|d{2})[-\\s]{1}[01]{1}d{1}[-\\s]{1}[0-3]{1}d{1}$";// 匹配日期
/**
* 匹配电话
*
* 格式为: 0XXX-XXXXXX(10-13位首位必须为0) 或0XXX XXXXXXX(10-13位首位必须为0) 或
* (0XXX)XXXXXXXX(11-14位首位必须为0) 或 XXXXXXXX(6-8位首位不为0) 或
* XXXXXXXXXXX(11位首位不为0)
*
* 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或
* 010-12345678 或 12345678912
*
* 不匹配: 1111-134355 或 0123456789
*
*/
public static final String phone_regexp = "^(?:0[0-9]{2,3}[-\\s]{1}|\\(0[0-9]{2,4}\\))[0-9]{6,8}$
|^[1-9]{1}[0-9]{5,7}$|^[1-9]{1}[0-9]{10}$";
/**
* 匹配身份证
*
* 格式为: XXXXXXXXXX(10位) 或 XXXXXXXXXXXXX(13位) 或 XXXXXXXXXXXXXXX(15位) 或
* XXXXXXXXXXXXXXXXXX(18位)
*
* 匹配 : 0123456789123
*
* 不匹配: 0123456
*
*/
public static final String ID_card_regexp = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";
/**
* 匹配邮编代码
*
* 格式为: XXXXXX(6位)
*
* 匹配 : 012345
*
* 不匹配: 0123456
*
*/
public static final String ZIP_regexp = "^[0-9]{6}$";// 匹配邮编代码
/**
* 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号>
左尖括号< 反斜杠\ 即空格,制表符,回车符等 )
*
* 格式为: x 或 一个一上的字符
*
* 匹配 : 012345
*
* 不匹配: 0123456
*
*/
public static final String non_special_char_regexp = "^[^'\"\\;,:-<>\\s].+$";// 匹配邮编代码
/**
* 匹配非负整数(正整数 + 0)
*/
public static final String non_negative_integers_regexp = "^\\d+$";
/**
* 匹配不包括零的非负整数(正整数 > 0)
*/
public static final String non_zero_negative_integers_regexp = "^[1-9]+\\d*$";
/**
*
* 匹配正整数
*
*/
public static final String positive_integer_regexp = "^[0-9]*[1-9][0-9]*$";
/**
*
* 匹配非正整数(负整数 + 0)
*
*/
public static final String non_positive_integers_regexp = "^((-\\d+)|(0+))$";
/**
*
* 匹配负整数
*
*/
public static final String negative_integers_regexp = "^-[0-9]*[1-9][0-9]*$";
/**
*
* 匹配整数
*
*/
public static final String integer_regexp = "^-?\\d+$";
/**
*
* 匹配非负浮点数(正浮点数 + 0)
*
*/
public static final String non_negative_rational_numbers_regexp = "^\\d+(\\.\\d+)?$";
/**
*
* 匹配正浮点数
*
*/
public static final String positive_rational_numbers_regexp = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)
|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
/**
*
* 匹配非正浮点数(负浮点数 + 0)
*
*/
public static final String non_positive_rational_numbers_regexp = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
/**
*
* 匹配负浮点数
*
*/
public static final String negative_rational_numbers_regexp = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)
|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
/**
*
* 匹配浮点数
*
*/
public static final String rational_numbers_regexp = "^(-?\\d+)(\\.\\d+)?$";
/**
*
* 匹配由26个英文字母组成的字符串
*
*/
public static final String letter_regexp = "^[A-Za-z]+$";
/**
*
* 匹配由26个英文字母的大写组成的字符串
*
*/
public static final String upward_letter_regexp = "^[A-Z]+$";
/**
*
* 匹配由26个英文字母的小写组成的字符串
*
*/
public static final String lower_letter_regexp = "^[a-z]+$";
/**
*
* 匹配由数字和26个英文字母组成的字符串
*
*/
public static final String letter_number_regexp = "^[A-Za-z0-9]+$";
/**
*
* 匹配由数字、26个英文字母或者下划线组成的字符串
*
*/
public static final String letter_number_underline_regexp = "^\\w+$";
/**
* 添加正规表达式 (以key->value的形式存储)
*
* @param regexpName
* 该正规表达式名称 `
* @param regexp
* 该正规表达式内容
*/
public void putRegexpHash(String regexpName, String regexp)
{
regexpHash.put(regexpName, regexp);
}
/**
* 得到正规表达式内容 (通过key名提取出value[正规表达式内容])
*
* @param regexpName
* 正规表达式名称
*
* @return 正规表达式内容
*/
public String getRegexpHash(String regexpName)
{
if (regexpHash.get(regexpName) != null)
{
return ((String) regexpHash.get(regexpName));
}
else
{
System.out.println("在regexpHash中没有此正规表达式");
return "";
}
}
/**
* 清除正规表达式存放单元
*/
public void clearRegexpHash()
{
regexpHash.clear();
return;
}
/**
* 大小写敏感的正规表达式批配
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回真,否则返回假 如: Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真
*/
public static boolean isHardRegexpValidate(String source, String regexp)
{
try
{
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regexp);
// 返回批配结果
return matcher.contains(source, hardPattern);
}
catch (MalformedPatternException e)
{
e.printStackTrace();
}
return false;
}