package demo.demo.test;
import java.util.regex.Pattern;
public class PatternCheck {
public static void main(String[] args) {
boolean b = checkCellphone("手机号码");
System.out.println(b?"是手机号":"不是手机号码");
boolean z = checkTelephone("区号-座机号");
System.out.println(z?"是座机号":"不是座机号码");
boolean m = checkMail("邮箱号码");
System.out.println(m?"是邮箱号":"不是邮箱号码");
boolean i = checkIdNo("身份证号码");
System.out.println(i?"是身份证号":"不是身份证号码");
}
/**
* 验证手机号码
*
* 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
* 联通号码段:130、131、132、136、185、186、145
* 电信号码段:133、153、180、189
*
* @param cellphone
* @return
*/
public static boolean checkCellphone(String cellphone) {
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
return check(cellphone, regex);
}
/**
* 验证固话号码
*
* @param telephone
* @return
*/
public static boolean checkTelephone(String telephone) {
String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
return check(telephone, regex);
}
/**
* 验证邮箱
*
* @param address
* @return
*/
public static boolean checkMail(String address) {
String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
return check(address, regex);
}
/**
* 验证身份证号
*
* @param idNo
* @return
*/
public static boolean checkIdNo(String idNo) {
String regex = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
return check(idNo, regex);
}
public static boolean check(String telephone, String regex) {
return Pattern.matches(regex, telephone);
}
}
身份证号/邮箱/手机号校验的正则表达式
最新推荐文章于 2024-06-17 16:15:26 发布