Android 正则表达式验证手机号码

时间:2025-02-04 08:04:25

方案一:比较精准的判断手机段位,但是随着手机号段的增多要不断的修改正则

 public  boolean isPhoneNumber1(String phone) {
String regExp = "^[1]([3][0-9]{1}|50|51|52|53|54|55|56|57|58|59|47|77|80|81|82|83|84|85|86|87|88|89)[0-9]{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(phone);
return m.find();//boolean
}

方案二:相对于方法一可以算得上一劳永逸,对140,141等目前不存在的号段没办法判断

 public  boolean isPhoneNumber2(String phone) {
String regExp = "^1[3|4|5|7|8]\\d{9}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(phone);
return m.find();
}