Title |
Test
Find
Pattern Title
|
Expression |
^[\(]? ([^0-1]){1}([0-9]){2}([-,\),/,\.])*([ ])?([^0-1]){1}([0-9]){2}[ ]?[-]?[/]?[\.]? ([0-9]){4}$ |
Description |
Here is a regular expression I wrote that validates U.S. phone numbers with area codes. You can't have a leading '0' or '1' but you can separate the number blocks with a space, a dot, a slash, or a hyphen. It also prevents a '0' or '1' for the first digit of the prefix. No extension is allowed and it must be 10 digits. |
Matches |
(713) 555-1212 | 713/555/1212 | 713.555 1212 |
Non-Matches |
07135551212 | 7130125551212 |
Author |
Rating:
Bruce Hatherly
|
Source |
|
Your Rating |
|
Title: Incorrect and inefficient syntax.
Name: g1smd
Date: 8/5/2012 4:03:11 AM
Comment:
The [\(]? simplifies to \(? here. No need for character group syntax when there is only one item in the group.
Specify the space using \s here.
The ([^0-1]){1} simplifies to [^01] here. The ( ) pair and the {1} are redundant.
The ([0-9]){2} simplifies to [0-9]{2} or \d{2} here. The ( ) pair are redundant.
The ([-,\),/,\.])* simplifies to [)/.-]* here. Character group items do not need comma separators, nor escaping and the ( ) pair are redundant
The ([ ])? simplifies to \s? here.
The [ ]?[-]?[/]?[\.]? simplifies to \s?-?/?\.? (no need for character group syntax when there is only one item in the group) or simplifies to [\s/.-]* here.
Title: Incorrect and inefficient syntax.
Name: g1smd
Date: 8/5/2012 4:01:33 AM
Comment:
The [\(]? simplifies to \(? here. No need for character group syntax when there is only one item in the group.
Specify the space using \s here.
The ([^0-1]){1} simplifies to [^01] here. The ( ) pair and the {1} are redundant.
The ([0-9]){2} simplifies to [0-9]{2} or \d{2} here. The ( ) pair are redundant.
The ([-,\),/,\.])* simplifies to [)/.-]* here. Character group items do not need comma separators, nor escaping and the ( ) pair are redundant
The ([ ])? simplifies to \s? here.
The [ ]?[-]?[/]?[\.]? simplifies to \s?-?/?\.? (no need for character group syntax when there is only one item in the group) or simplifies to [\s/.-]* here.