本文共 9354 字,大约阅读时间需要 31 分钟。
java正则表达式
一、正则表达式常用的构造元素 (1)字符 x 字符 x。举例:'a'表示字符a \\ 反斜线字符。 \n 新行(换行)符 ('\u000A') \r 回车符 ('\u000D') (2)字符类 [abc] a、b 或 c(简单类) [^abc] 任何字符,除了 a、b 或 c(否定) [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) [0-9] 0到9的字符都包括 (3)预定义字符类 . 任何字符。我的就是.字符本身,怎么表示呢? \. \d 数字:[0-9] \D 非数字:[^\d]/[^0-9] \w 单词字符:[a-zA-Z_0-9] \W 非字符[^\w] \s空白符 \S非空白符 (4)边界匹配器 ^ 行的开头 $ 行的结尾 \b 单词边界, 就是不是单词字符的地方。 (5)Greedy 数量词 X? X,一次或一次也没有 X* X,零次或多次 X+ X,一次或多次 X{n} X,恰好 n 次 X{n,} X,至少 n 次 X{n,m} X,至少 n 次,但是不超过 m 次 (6)运算符 XY X后跟 Y X|Y X 或 Y (X) X,作为捕获组 二、String类使用正则表达式 匹配:matches() 切割:split() 替换:replaceAll() and replaceFirst() 使用例子如下:String strOfMatches = "-1234";String regex = "-?\\d+";System.out.println(strOfMatches.matches(regex));//out trueString strOfReplace = "jack rose alen";regex = "k?\\s+";System.out.println(strOfReplace.replaceAll(regex,"***"));//out jac***rose***alenSystem.out.println(Arrays.toString(strOfReplace.split(regex)));//out [jac, rose, alen]
三、Pattern正则表达式的使用
//正则表达式使用二 Pattern pattern = Pattern.compile("-?\\d+"); Matcher matcher = pattern.matcher("-1234 -123 123"); while (matcher.find()){ System.out.println(matcher.group()); }
从上面可以看出使用matcher.find()获取的是一个匹配组。
三、常用正则表达式工具类package com.liutao.regex;import java.util.regex.Matcher;import java.util.regex.Pattern;public final class RegExpValidatorUtils { /** * 验证邮箱 * * @param 待验证的字符串 * @return 如果是符合的字符串,返回 true ,否则为 false */ public static boolean isEmail(String str) { String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; return str.matches(regex); } /** * 验证IP地址 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean isIP(String str) { String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$"; return str.matches(regex); } /** * 验证网址Url * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsUrl(String str) { String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"; return str.matches(regex); } /** * 验证电话号码 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsTelephone(String str) { String regex = "^(\\d{3,4}-)?\\d{6,8}$"; return str.matches(regex); } /** * 验证输入密码条件(字符与数据同时出现) * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsPassword(String str) { String regex = "[A-Za-z]+[0-9]"; return str.matches(regex); } /** * 验证输入密码长度 (6-18位) * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsPasswLength(String str) { String regex = "^\\d{6,18}$"; return str.matches(regex); } /** * 验证输入邮政编号 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsPostalcode(String str) { String regex = "^\\d{6}$"; return str.matches(regex); } /** * 验证输入手机号码 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsHandset(String str) { String regex = "^[1]+[3,5]+\\d{9}$"; return str.matches(regex); } /** * 验证输入身份证号 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsIDcard(String str) { String regex = "(^\\d{18}$)|(^\\d{15}$)"; return str.matches(regex); } /** * 验证输入两位小数 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsDecimal(String str) { String regex = "^[0-9]+(.[0-9]{2})?$"; return str.matches(regex); } /** * 验证输入一年的12个月 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsMonth(String str) { String regex = "^(0?[[1-9]|1[0-2])$"; return str.matches(regex); } /** * 验证输入一个月的31天 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsDay(String str) { String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$"; return str.matches(regex); } /** * 验证日期时间 * * @param 待验证的字符串 * @return 如果是符合网址格式的字符串,返回 true ,否则为 false */ public static boolean isDate(String str) {// 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],// [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])// String regex =// "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";// 没加时间验证的YYYY-MM-DD// String regex =// "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";// 加了时间验证的YYYY-MM-DD 00:00:00 String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; return str.matches(regex); } /** * 验证数字输入 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsNumber(String str) { String regex = "^[0-9]*$"; return str.matches(regex); } /** * 验证非零的正整数 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsIntNumber(String str) { String regex = "^\\+?[1-9][0-9]*$"; return str.matches(regex); } /** * 验证大写字母 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsUpChar(String str) { String regex = "^[A-Z]+$"; return str.matches(regex); } /** * 验证小写字母 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsLowChar(String str) { String regex = "^[a-z]+$"; return str.matches(regex); } /** * 验证验证输入字母 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsLetter(String str) { String regex = "^[A-Za-z]+$"; return str.matches(regex); } /** * 验证验证输入汉字 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsChinese(String str) { String regex = "^[\u4e00-\u9fa5],{0,}$"; return str.matches(regex); } /** * 验证验证输入字符串 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 true ,否则为 false */ public static boolean IsLength(String str) { String regex = "^.{8,}$"; return str.matches(regex); } /** * @param regex * 正则表达式字符串 * @param str * 要匹配的字符串 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false; */ private static boolean match(String regex, String str) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.matches(); }}
五、常用正则表达式
匹配特定数字: ^[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个英文字母或者下划线组成的字符串 在使用RegularExpressionValidator验证控件时的验证功能及其验证表达式介绍如下: 只能输入数字:“^[0-9]*$” 只能输入n位的数字:“^d{n}$” 只能输入至少n位数字:“^d{n,}$” 只能输入m-n位的数字:“^d{m,n}$” 只能输入零和非零开头的数字:“^(0|[1-9][0-9]*)$” 只能输入有两位小数的正实数:“^[0-9]+(.[0-9]{2})?$” 只能输入有1-3位小数的正实数:“^[0-9]+(.[0-9]{1,3})?$” 只能输入非零的正整数:“^+?[1-9][0-9]*$” 只能输入非零的负整数:“^-[1-9][0-9]*$” 只能输入长度为3的字符:“^.{3}$” 只能输入由26个英文字母组成的字符串:“^[A-Za-z]+$” 只能输入由26个大写英文字母组成的字符串:“^[A-Z]+$” 只能输入由26个小写英文字母组成的字符串:“^[a-z]+$” 只能输入由数字和26个英文字母组成的字符串:“^[A-Za-z0-9]+$” 只能输入由数字、26个英文字母或者下划线组成的字符串:“^w+$” 验证用户密码:“^[a-zA-Z]w{5,17}$”正确格式为:以字母开头,长度在6-18之间, 只能包含字符、数字和下划线。 验证是否含有^%&’,;=?$”等字符:“[^%&’,;=?$x22]+” 只能输入汉字:“^[u4e00-u9fa5],{0,}$” 验证Email地址:“^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$” 验证InternetURL:“^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$” 验证电话号码:“^((d{3,4})|d{3,4}-)?d{7,8}$” 正确格式为:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”, “XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。 验证身份证号(15位或18位数字):“^d{15}|d{}18$” 验证一年的12个月:“^(0?[1-9]|1[0-2])$”正确格式为:“01”-“09”和“1”“12” 验证一个月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$” 正确格式为:“01”“09”和“1”“31”。 匹配中文字符的正则表达式: [u4e00-u9fa5] 匹配双字节字符(包括汉字在内):[^x00-xff] 匹配空行的正则表达式:n[s| ]*r 匹配HTML标记的正则表达式:/< (.*)>.*|< (.*) />/ 匹配首尾空格的正则表达式:(^s*)|(s*$) 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 匹配网址URL的正则表达式:http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
转载地址:http://dukgj.baihongyu.com/