Solved

regular expressions - phone number and extension

Posted on 2009-04-09
4
1,023 Views
Last Modified: 2012-05-06
We are trying to use regular expressions to check a valid phone number.  We have a pattern but we want to add an option for an extension.
phonePattern = @"\((?<AreaCode>\d{3}) \)?[-\s]? (?<Prefix>\d{3}) [-.] (?<Number>\d{4})";
would this work
phonePattern = @"\((?<AreaCode>\d{3}) \)?[-\s]? (?<Prefix>\d{3}) [-.] (?<Number>\d{4}) x?<ext>\d{10}";
Here are some valid examples for phone numbers
555-123-4567
555-123-4567 x1234
555-123-4567 x 1234
555-123-4567 x778942
0
Comment
Question by:tampsystems
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:tosanjay
ID: 24108489
ok. So, valid examples are:
555-123-4567
555-123-4567 x1234
555-123-4567 x 1234
555-123-4567 x778942
e.g. <3 digits><-><3 digits><-><4 digits>#extension Part#<space><x><optional space><4 to 6 digits>

let me put regexp in a simpler form.
\d{3}\-\d{3}\-\d{4}(\sx\s?\d{4,6})?

this will match all the number formats, mentioned above. A better solution can be made if you know the next character after a number e.g. new line/carriage return.
Hope this helps.
0
 

Author Comment

by:tampsystems
ID: 24116526
I want to be able to support () spaces and dashes
i.e.
(555) 123 4567
(555) 123-4567
555 123 4567
555 123-4567
555-123-4567
5551234567
555 1234567
555123 4567
And the optional extension number
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24119510
The following regular expression matches your formats, and the following:

(123) 123-4567 1234
(123) 1234567 1234
(123) 123-4567
(123) 1234567
123-4567 1234
1234567 1234
123-4567
1234567

((\d{3}|\(\d{3}\))?(\s|\-)?\d{3}(\s|\-)?\d{4})((\s|\-)?\d{4})?

Open in new window

0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 24119551
Sorry, I just noticed that you wanted an "x" on your extension number.

The below regex matches:

(123) 123-4567 x1234
(123) 1234567 x1234
123-4567 x1234
1234567 x1234

(123) 123-4567 x 1234
(123) 1234567 x 1234
123-4567 x 1234
1234567 x 1234

(123) 123-4567x 1234
(123) 1234567x 1234
123-4567x 1234
1234567x 1234

(123) 123-4567x1234567890
(123) 1234567x1234567890
123-4567x1234567890
1234567x1234567890

((\d{3}|\(\d{3}\))?(\s|\-)?\d{3}(\s|\-)?\d{4})(\s?x(\s)?\d{1,10})?

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question