/**
* 正则表达式工具类
*/
public class RegexpUtils {
/**
* 验证手机号
*/
public static final String PHONE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
public static final String PHONE2 = "^1\\d{10}$";
/**
* 验证邮箱地址
*/
public static final String EMAIL = "\\w+(\\.\\w)*@\\w+(\\.\\w{2,3}){1,3}";
/**
* 验证正整数
*/
public static final String PURE_DIGITAL = "[0-9]*";
/**
* 验证手机号
*
* @param phone 手机号
* @return 校验结果
*/
public static boolean checkPhone(String phone) {
return phone.matches(PHONE);
}
public static boolean checkPhone2(String phone) {
return phone.matches(PHONE2);
}
/**
* 验证邮箱
*
* @param email 邮箱
* @return 校验结果
*/
public static boolean checkEmail(String email) {
return email.matches(EMAIL);
}
/**
* 验证纯数字
*
* @param digital 数字
* @return 校验结果
*/
public static boolean checkPureDigital(String digital) {
return digital.matches(PURE_DIGITAL);
}
/**
* 判断字符串是否全部为中文字符组成
*
* @param str 检测的文字
* @return true:为中文字符串,false:含有非中文字符
*/
public static boolean isChineseStr(String str) {
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
char c[] = str.toCharArray();
for (int i = 0; i < c.length; i++) {
Matcher matcher = pattern.matcher(String.valueOf(c[i]));
if (!matcher.matches()) {
return false;
}
}
return true;
}
/**
* 判断是否含有特殊字符
*
* @param str
* @return true为包含,false为不包含
*/
public static boolean isSpecialChar(String str) {
String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
}
java正则表达式工具类
最新推荐文章于 2023-07-03 16:41:14 发布