用途:用来处理字符串的,判断其是否属于某种文本
功能:
1.验证电话号码:isTel
2.验证身份证号码18位:isIDCard18
3.验证手机号(简单):isMobileSimple
4.验证手机号(精确):isMobileExact
5.验证邮箱:isEmail
6.验证URL:isURL
7.验证汉字:isChinesec
8.验证yyyy-MM-dd格式的日期校验:isDate
9.验证IP地址:isIP
10.判断是否匹配正则:estimate
public class RegexUtils {
private RegexUtils() {
}
/**
* 验证电话号码
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isTel(CharSequence input) {
return estimate("^0\\d{2,3}[- ]?\\d{7,8}", input);
}
/**
* 验证身份证号码18位
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isIDCard18(CharSequence input) {
return estimate("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$", input);
}
/**
* 验证手机号(简单)
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isMobileSimple(CharSequence input) {
return estimate("^[1]\\d{10}$", input);
}
/**
* 验证手机号(精确)
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isMobileExact(CharSequence input) {
return estimate("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$", input);
}
/**
* 验证邮箱
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isEmail(CharSequence input) {
return estimate("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", input);
}
/**
* 验证URL
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isURL(CharSequence input) {
return estimate("[a-zA-z]+://[^\\s]*", input);
}
/**
* 验证汉字
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isChinesec(CharSequence input) {
return estimate("^[\\u4e00-\\u9fa5]+$", input);
}
/**
* 验证yyyy-MM-dd格式的日期校验
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isDate(CharSequence input) {
return estimate("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$", input);
}
/**
* 验证IP地址
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isIP(CharSequence input) {
return estimate("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)", input);
}
/**
* 判断是否匹配正则
*
* @param regex 正则表达式
* @param input 要匹配的字符串
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean estimate(String regex, CharSequence input) {
return input != null && input.length() > 0 && Pattern.matches(regex, input);
}
}