1

Possible Duplicate:
A comprehensive regex for phone number validation

I need a regular expression that accepts all following phone number formats:

+1-650-123-0123

(650) 123 0123

(650) 123-0123

(+1): 650-123-0123

(+1) 650 123 0123

I currently have the following regular expression:

\(?\+?[0-9]?\)?-?\(?(\d{3})\)?(\s|-)+(\d{3})(\s|-)+(\d{4})

The problem I currently have is that it accepts most of them, but not the following: (650)123-0123. The part in the middle ((\s|-)+) should only match if it found (xyz).. How can I do this? I tried doing a lookahead/lookbehind, but I can't get it to work.

Thanks!

Community
  • 1
  • 1
philipDS
  • 661
  • 3
  • 7
  • 16
  • 3
    Please see [this](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation). –  Mar 20 '12 at 13:15
  • @edmastermind29, This is not what he wants. He has to *validate* given phone number, not *normailze* it. – AlexR Mar 20 '12 at 13:22
  • You can use multiple regexp and OR them. In some cases multiple simple regexp are easier to debug. – Tony Ennis Mar 20 '12 at 13:26
  • I'm not sure but it looks like you are expecting a whitespace or - after the (650) with (\s|-)+ – cagla Mar 20 '12 at 13:22
  • Yes, but I only want a whitespace or - if it found an occurence of something like (650) before.. Is this possible? – philipDS Mar 20 '12 at 13:33

3 Answers3

3

There is a project on Google code that has solved your problem for you!

Their solution has been ported to Python.

Highlights of functionality

Parsing/formatting/validating phone numbers for all countries/regions of the world.

getNumberType - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).

isNumberMatch - gets a confidence level on whether two numbers could be the same.

getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.

isPossibleNumber - quickly guessing whether a number is a possible phonenumber by using only the length information, much faster than a full validation.

isValidNumber - full validation of a phone number for a region using length and prefix information.

AsYouTypeFormatter - formats phone numbers on-the-fly when users enter each digit.

findNumbers - finds numbers in text input.

PhoneNumberOfflineGeocoder - provides geographical information related to a phone number.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Katriel
  • 120,462
  • 19
  • 136
  • 170
1

You could use conditional look aheads like (?(?=regex)then|else) but AFAIK Java's standard regex engine doesn't support those.

Alternatively build a couple of expressions and test if any one of them matches. This would make reading/writing the expressions easier and might not even have a noticable performance hit. If you need all those in a single expression try something like this:

^expression1$|^expression2$|...

Note, however, that this might result in some problems if the expressions get too complicated (some JVM versions seem to have difficulties with the | (or) expressions.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Yeah.. I thought about building a couple of expressions. It looks like Java doesn't support look aheads.. Thanks. – philipDS Mar 20 '12 at 13:34
  • @durplr Java supports look aheads but it doesn't support conditions based on look aheads. – Thomas Mar 20 '12 at 13:42
0

Please check my source below. That will give you many starting points in which to construct your regex for phone numbers.

Here is my source.

Community
  • 1
  • 1
  • Copy paste is not good excercise. – Chandra Sekhar Mar 20 '12 at 13:18
  • I don't want a completely new regular expression. I want my thought process and add a few things or change something so that it works. Copying your regex won't help me. The whole idea is that I learn how to fix my final regular expression. Thanks for the answer anyway. – philipDS Mar 20 '12 at 13:23
  • @durplr You're welcome. I believe its a starting point that leads to fixing your regex. You can use [this](http://regexpal.com/) to test what you come up with. –  Mar 20 '12 at 13:26