java 正则验证手机号码 转载
程序员文章站
2022-04-04 12:54:26
...
public static boolean validateMoblie(String phone) {
int l = phone.length();
boolean rs=false;
switch (l) {
case 7:
if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4}$", phone)) {
rs= true;
}
break;
case 11:
if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4,8}$", phone)) {
rs= true;
}
break;
default:
rs=false;
break;
}
return rs;
}
private static boolean matchingText(String expression, String text) {
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(text); // 操作的字符串
boolean b = m.matches();
return b;
}
public static void main(String[] args) {
System.out.println(validateMoblie("13556898956"));
}