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.
For more than 100 detailed regular expression recipes that include equal coverage for eight programming languages (C#, Java, Java Script, Perl, PHP, Python, Ruby, and VB. We’ll explain more about the question mark after discussing the other types of tokens in this regular expression.
NET), get your very own copy of , so that your phone number records are consistent. The parentheses that appear without backslashes are capturing groups and are used to remember the values matched within them so that the matched text can be recalled later.
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.
⋯ # [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.
Note that although this recipe claims to handle North American phone numbers, it’s actually designed to work with (NANP) numbers.
The NANP is the telephone numbering plan for the countries that share the country code “1”.
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.
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.Tags: Adult Dating, affair dating, sex dating