Regular expression for validating international phone numbers
‘$’ is used to stop matching too much string into final result than required., 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.
Note that the first word boundary token appears after the optional, opening parenthesis.
By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want. Two other types of tokens used in this regular expression are character classes and quantifiers.
See Recipes 3.5 and 3.15 for help implementing this regular expression with other programming languages. Character classes allow you to match any one out of a set of characters.
This is important because there is no word boundary to be matched between two nonword characters, such as the opening parenthesis and a preceding space character.
The first word boundary is relevant only when matching a number without parentheses, since the word boundary always matches between the opening parenthesis and the first digit of a phone number.