Regular expression for validating international phone numbers
, composing a fairly comprehensive guide to validating and formatting North American and international phone numbers using regular expressions. Thus, the sequences that enclose the first group of digits match literal parenthesis characters.The regexes in these recipes are all pretty straightforward, but hopefully this gives an example of the depth you can expect from the book. Both are followed by a question mark, which makes them optional.The “1” in this pattern is preceded by an optional plus sign, and optionally followed by one of three separators (hyphen, dot, or space).The entire, added noncapturing group is also optional, but since the “1” is required within the group, the preceding plus sign and separator are not allowed if there is no leading “1”.More information is available at See Recipe 3.5 for help implementing this regular expression with other programming languages. This regular expression follows the international phone number notation specified by the Extensible Provisioning Protocol (EPP).The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless you adopt a strict format. # Repeat the preceding group between 6 and 14 times. EPP is a relatively recent protocol (finalized in 2004), designed for communication between domain name registries and registrars.So far, the regular expression matches any 10-digit number.
This regular expression matches three groups of digits. ( # Capture the enclosed match to backreference 1... is another character class, one that allows any one of three separators.
⋯ # [Match the remaining digits and separator.] $ # Assert position at the end of the string..
Since a question mark is used after each separator, the phone number digits are allowed to run together.
This is a textbook example of where we need a backslash to escape a special character so the regular expression treats it as literal input.
As we’ve repeatedly seen, parentheses are special characters in regular expressions, but in this case we want to allow a user to enter parentheses and have our regex recognize them.
When a question mark follows an unescaped left parenthesis like this, it’s not a quantifier, but instead helps to identify the type of grouping.