To allow matching phone numbers that omit the local area code, enclose the first group of digits together with its surrounding parentheses and following separator in an optional, noncapturing group: , with an empty set of parentheses.
To work around this, add code outside the regex that checks whether group 1 matched any text, and adjust the replacement text accordingly.
Unless you have very specific needs that require you to filter out as many phone numbers as possible, don’t go overboard trying to eliminate unused numbers.
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”.
, 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.
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.Two simple changes allow the previous regular expression to match phone numbers within longer text: matches the position between a word character and either a nonword character or the beginning or end of the text.Letters, numbers, and underscore are all considered word characters (see Recipe 2.6).Note that the first word boundary token appears after the optional, opening parenthesis.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.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).