RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

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: The rating for this expression. Bruce Hatherly
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

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.


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials