public class PhoneNumberValidator {
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
private static final String[] PHONE_SEGMENTS =
{
"130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "141", "145", "146", "147", "148",
"149", "150", "151", "152", "153", "155", "156", "157", "158", "159", "162", "165", "166", "167", "170",
"171", "172", "173", "174", "175", "176", "177", "178", "180", "181", "182", "183", "184", "185", "186",
"187", "188", "189", "190", "191", "192", "193", "195", "196", "197", "198", "199"
};
public static boolean isValidPhoneNumberFormat(String phoneNumber) {
if (phoneNumber == null) {
return false;
}
return phoneNumber.matches(PHONE_REGEX);
}
public static boolean isCompliantPhoneNumber(String phoneNumber) {
if (!isValidPhoneNumberFormat(phoneNumber)) {
return false;
}
for (String segment : PHONE_SEGMENTS) {
if (phoneNumber.startsWith(segment)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String phoneNumber = "19941192703";
System.out.println("手机号格式是否正确:" + isValidPhoneNumberFormat(phoneNumber));
System.out.println("手机号是否合规:" + isCompliantPhoneNumber(phoneNumber));
}
}