public static boolean validateInternationalPhoneNumberFormat(String phone) {
StringBuilder sb = new StringBuilder(200);
// Country code
sb.append("^(\\+{1}[\\d]{1,3})?");
// Area code, with or without parentheses
sb.append("([\\s])?(([\\(]{1}[\\d]{2,3}[\\)]{1}[\\s]?)|([\\d]{2,3}[\\s]?))?");
// Phone number separator can be "-", "." or " "
// Minimum of 5 digits (for fixed line phones in Solomon Islands)
sb.append("\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?");
// 4 more optional digits
sb.append("\\d?[\\-\\.\\s]?\\d?[\\-\\.\\s]?\\d?[\\-\\.\\s]?\\d?$");
return Pattern.compile(sb.toString()).matcher(phone).find();
}
^ - start of expression
(?!\b(0)\1+\b) - (?!)Negative Look ahead. \b - word boundary around a '0' character. \1 backtrack to previous capturing group (zero). Basically don't match all zeros.
(\+?\d{1,3}[. -]?)? - '\+?' plus sign before country code is optional.\d{1,3} - country code can be 1 to 3 digits long. '[. -]?' - spaces,dots and dashes are optional. The last question mark is to make country code optional.
\(?\d{3}\)? - '\)?' is to make parentheses optional. \d{3} - match 3 digit area code.
([. -]?) - optional space, dash or dot
$ - end of expression