java正则表达式
1,表示单个字符(每出现一次只能表示一位)
x:表示只能有x组成
\\:表示转义字符\
\t:匹配tab键
\n:匹配换行
2,表示字符的选用范围
[abc]:表示a,b,c当中的任意一个
[^abc]:表示不是a,b,c当中的任意一个
[a-zA-Z]:表示任意一个字母,可以是小写可以是大写
[0-9]:表示任意一个数字
.:表示任意一个字符
\\.:表示一个.
3,简化表达式(每出现一次只能表示一位)
\\d:表示一位数字,相当于[0-9]
\\D:表示不是一位数字,相当于[^0-9]
\\s:表示任意一位空格
\\S:表示不是任意一位空格
\\w:表示字母,数字,_中的任意一位,相当于[0-9a-zA-Z_]
\\W:表示不是字母,数字,_中的任意一位,相当于[^0-9a-zA-Z_]
%s:专指单字节字符的字符串
4,数量表示:如果没有数量,每个出现的标识只能表示一位
正则?:表示此正则规范只能出现0次或1次
正则*:表示此正则规范只能出现0次或1次或多次
正则+:表示此正则规范只能出现1次或多次
正则{n}:表示此正则规范正好出现n次
正则{n,}:表示此正则规范出现n次以上
正则{n,m}:表示此正则规范出现n-m次
5,逻辑操作符
正则X正则Y :表示正则X判断后执行正则Y
正则X|正则Y :表示两个正则二选一
(正则):包含多个正则,成为一组
6,String中使用正则:
案例1:匹配出生日期
String str ="1998-08-02";
String regex = "\\d{4}-\\d{2}-\\d{2}";
System.out.println(str.matches(rege2x));
案例2:匹配分数
String str ="90.6";
String regex = "\\d{1,3}(\\.\\d{1,2})?";
System.out.println(str.matches(rege2x));
RegexUtils:
package demo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*
* 正则工具类 提供验证邮箱,手机号,电话号,身份证号,数据等方法
*
*/
public final class RegexUtils {
public final static String NUMERIC_REGEX = "(.*[NUMERIC|INT].*)";
public final static String VARCHAR_REGEX = "(.*[VARCHAR|STRING].*)";
/**
* 验证字符串类型
*
*
*/
public static boolean checkVacharType(String str){
Pattern pattern = Pattern.compile(VARCHAR_REGEX);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
/**
* 验证邮箱
*
* email地址,格式:renpl@sina.com zhangsan@xxx.com.cn xxx代表邮箱服务商
*
*/
public static boolean checkEmail(String email){
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
return Pattern.matches(regex, email);
}
/**
* 验证身份证号
*
* 居民身份证号是15位或者18位,最后一位可能是数字或者字母
*
*/
public static boolean checkIdCard(String idCard){
String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex, idCard);
}
/**
* 验证手机号
*
* 移动,联通,电信运营商的号码段
*
* 移动:134(0-8),135,136,137,138,139,147(预计用于TD上网卡),150,151,152,157(TD专用),158,159,187(未启用),188(TD专用)
*
* 联通:130,131,132,155,156(世界风专用),185(未启用),186(3G)
*
* 电信:133,153,180(未启用),189
*
*
*/
public static boolean checkMobile(String mobile){
String regex = "(\\+\\d+)?1[3458]\\d{9}$";
return Pattern.matches(regex, mobile);
}
/**
* 验证固定电话号
*
* 国家(地区)代码+电话号码
*
*区号(城市代码)+电话号码
*
* 电话号码:从0到9的一个或多个数字
*
*/
public static boolean checkPhone(String phone){
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
return Pattern.matches(regex, phone);
}
/**
* 验证整数
*
* 一位或多位0-9之间的整数
*
*/
public static boolean checkDigit(String digit){
String regex = "\\-?[1-9]\\d+";
return Pattern.matches(regex, digit);
}
/**
* 验证整数和浮点数 (正负整数和正负浮点数)
* 一位或多位0-9之间的浮点数 如:1.23 23.40
*
*/
public static boolean checkDecimals(String decimals){
String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
return Pattern.matches(regex, decimals);
}
/**
* 验证空白字符
*
* 空白字符包括:空格,\t,\n,\r,\f,\x0B
*
*/
public static boolean checkBlankSpace(String blankSpace){
String regex = "\\s+";
return Pattern.matches(regex, blankSpace);
}
/**
* 验证中文
*
* 中文字符
*
*/
public static boolean checkChinese(String chinese){
String regex = "^[\u4E00-\u9FA5]+$";
return Pattern.matches(regex, chinese);
}
/**
* 验证日期(年月日)
*
* 日期 格式:1992-09-03或者1992.09.03或者1992/09/03
*
*/
public static boolean checkBirthday(String birthday){
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
return Pattern.matches(regex, birthday);
}
/**
* 验证URL地址
*
* url 格式:1992-09-03或者1992.09.03或者1992/09/03
*
*/
public static boolean checkURL(String url){
String regex = "(https?://(w{3}\\.)?}?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*))?(&.+=.*)?)?";
return Pattern.matches(regex, url);
}
/**
* 验证中国邮政编码
*
* 格式:100000
*
*/
public static boolean checkPostcode(String posecode){
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, posecode);
}
/**
* 验证ipAddress
*
* 这里只匹配IPv4标准地址
*
*/
public static boolean checkIP(String ip){
String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
return Pattern.matches(regex, ip);
}
}
1,表示单个字符(每出现一次只能表示一位)
x:表示只能有x组成
\\:表示转义字符\
\t:匹配tab键
\n:匹配换行
2,表示字符的选用范围
[abc]:表示a,b,c当中的任意一个
[^abc]:表示不是a,b,c当中的任意一个
[a-zA-Z]:表示任意一个字母,可以是小写可以是大写
[0-9]:表示任意一个数字
.:表示任意一个字符
\\.:表示一个.
3,简化表达式(每出现一次只能表示一位)
\\d:表示一位数字,相当于[0-9]
\\D:表示不是一位数字,相当于[^0-9]
\\s:表示任意一位空格
\\S:表示不是任意一位空格
\\w:表示字母,数字,_中的任意一位,相当于[0-9a-zA-Z_]
\\W:表示不是字母,数字,_中的任意一位,相当于[^0-9a-zA-Z_]
%s:专指单字节字符的字符串
4,数量表示:如果没有数量,每个出现的标识只能表示一位
正则?:表示此正则规范只能出现0次或1次
正则*:表示此正则规范只能出现0次或1次或多次
正则+:表示此正则规范只能出现1次或多次
正则{n}:表示此正则规范正好出现n次
正则{n,}:表示此正则规范出现n次以上
正则{n,m}:表示此正则规范出现n-m次
5,逻辑操作符
正则X正则Y :表示正则X判断后执行正则Y
正则X|正则Y :表示两个正则二选一
(正则):包含多个正则,成为一组
6,String中使用正则:
案例1:匹配出生日期
String str ="1998-08-02";
String regex = "\\d{4}-\\d{2}-\\d{2}";
System.out.println(str.matches(rege2x));
案例2:匹配分数
String str ="90.6";
String regex = "\\d{1,3}(\\.\\d{1,2})?";
System.out.println(str.matches(rege2x));
RegexUtils:
package demo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*
* 正则工具类 提供验证邮箱,手机号,电话号,身份证号,数据等方法
*
*/
public final class RegexUtils {
public final static String NUMERIC_REGEX = "(.*[NUMERIC|INT].*)";
public final static String VARCHAR_REGEX = "(.*[VARCHAR|STRING].*)";
/**
* 验证字符串类型
*
*
*/
public static boolean checkVacharType(String str){
Pattern pattern = Pattern.compile(VARCHAR_REGEX);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
/**
* 验证邮箱
*
* email地址,格式:renpl@sina.com zhangsan@xxx.com.cn xxx代表邮箱服务商
*
*/
public static boolean checkEmail(String email){
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
return Pattern.matches(regex, email);
}
/**
* 验证身份证号
*
* 居民身份证号是15位或者18位,最后一位可能是数字或者字母
*
*/
public static boolean checkIdCard(String idCard){
String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex, idCard);
}
/**
* 验证手机号
*
* 移动,联通,电信运营商的号码段
*
* 移动:134(0-8),135,136,137,138,139,147(预计用于TD上网卡),150,151,152,157(TD专用),158,159,187(未启用),188(TD专用)
*
* 联通:130,131,132,155,156(世界风专用),185(未启用),186(3G)
*
* 电信:133,153,180(未启用),189
*
*
*/
public static boolean checkMobile(String mobile){
String regex = "(\\+\\d+)?1[3458]\\d{9}$";
return Pattern.matches(regex, mobile);
}
/**
* 验证固定电话号
*
* 国家(地区)代码+电话号码
*
*区号(城市代码)+电话号码
*
* 电话号码:从0到9的一个或多个数字
*
*/
public static boolean checkPhone(String phone){
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
return Pattern.matches(regex, phone);
}
/**
* 验证整数
*
* 一位或多位0-9之间的整数
*
*/
public static boolean checkDigit(String digit){
String regex = "\\-?[1-9]\\d+";
return Pattern.matches(regex, digit);
}
/**
* 验证整数和浮点数 (正负整数和正负浮点数)
* 一位或多位0-9之间的浮点数 如:1.23 23.40
*
*/
public static boolean checkDecimals(String decimals){
String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
return Pattern.matches(regex, decimals);
}
/**
* 验证空白字符
*
* 空白字符包括:空格,\t,\n,\r,\f,\x0B
*
*/
public static boolean checkBlankSpace(String blankSpace){
String regex = "\\s+";
return Pattern.matches(regex, blankSpace);
}
/**
* 验证中文
*
* 中文字符
*
*/
public static boolean checkChinese(String chinese){
String regex = "^[\u4E00-\u9FA5]+$";
return Pattern.matches(regex, chinese);
}
/**
* 验证日期(年月日)
*
* 日期 格式:1992-09-03或者1992.09.03或者1992/09/03
*
*/
public static boolean checkBirthday(String birthday){
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
return Pattern.matches(regex, birthday);
}
/**
* 验证URL地址
*
* url 格式:1992-09-03或者1992.09.03或者1992/09/03
*
*/
public static boolean checkURL(String url){
String regex = "(https?://(w{3}\\.)?}?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*))?(&.+=.*)?)?";
return Pattern.matches(regex, url);
}
/**
* 验证中国邮政编码
*
* 格式:100000
*
*/
public static boolean checkPostcode(String posecode){
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, posecode);
}
/**
* 验证ipAddress
*
* 这里只匹配IPv4标准地址
*
*/
public static boolean checkIP(String ip){
String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
return Pattern.matches(regex, ip);
}
}