So far, the regular expression matches any 10-digit number.
If you want to limit matches to valid phone numbers according to the North American Numbering Plan, here are the basic rules: Beyond the basic rules just listed, there are a variety of reserved, unassigned, and restricted phone numbers.
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.
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.
New area codes that fit the rules listed earlier are made available regularly, and even if a phone number is valid, that doesn’t necessarily mean it was issued or is in active use.
This includes the United States and its territories, Canada, Bermuda, and 16 Caribbean nations.
The North American Numbering Plan (NANP) is the telephone numbering plan for the United States and its territories, Canada, Bermuda, and 16 Caribbean nations.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.Standard capturing groups require the regular expression engine to keep track of backreferences, so it’s more efficient to use noncapturing groups whenever the text matched by a group does not need to be referenced later. 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.
The first group can optionally be enclosed with parentheses, and the first two groups can optionally be followed with a choice of three separators (a hyphen, dot, or space). It’s important that the hyphen appears first in this character class, because if it appeared between other characters, it would create a range, as with .