实现手机号、邮箱的正则验证
1. 手机号正则验证实现
/**
* 检查手机格式
*
* @param phone 手机号
* @return Boolean
*/
public static final Boolean checkPhoneFromat(String phone) {
/*
* 正则:手机号(精确)
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188、198</p>
* <p>联通:130、131、132、145、155、156、175、176、185、186、166</p>
* <p>电信:133、153、173、177、180、181、189、199</p>
* <p>全球星:1349</p>
* <p>虚拟运营商:170</p>
*/
String check = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(phone);
boolean isMatched = matcher.matches();
return isMatched;
}
2. 邮箱正则校验
/**
* 检查邮箱格式
*
* @param email 邮箱
* @return Boolean
*/
public static final Boolean checkEmailFormat(String email) {
String check = "^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
boolean isMatched = matcher.matches();
return isMatched;
}
3. 结语
记录,便于后期查找