1

Possible Duplicate:
A comprehensive regex for phone number validation

I've spent about two days searching for a regex that will find international phone numbers (using in scraping script), but I've run into a few problems..

  • I don't know the format(s), if they are similar lengths, etc..
  • I suck at complex regex

I had something that I thought worked, but then I saw a phone number from the UK that had a (0) before the 'area' code, which I've been told is not super common, but is used for within/outside numbers..

So what I need is a regex to be used in my Ruby script that will detect as many International phone numbers as possible, which accounts for this '(0)', and the possibility of a '+' in front of country code...

Because this is for scraping, and I can assume people enter phone numbers in a formatted way, I can expect a space, a ., or a - in between the area code and last 4 digits..

EDIT - This is what I tried, and wasn't getting results with which led me here..

/((\+\d{1,3})[- .]?(\d{2})[- .]?)(\d{3,4})[- .]?(\d{4})/
Community
  • 1
  • 1
Rabbott
  • 4,282
  • 1
  • 30
  • 53
  • international phone numbers can be quoted in a large number of different formats. The dot format (which I see a lot of) looks something like this: `44.1234567890` where `44` is the international prefix. As you can see, in the absence of context, it would be impossible to determine for sure that this is a phone number. To answer your point about the zero: Many (most?) countries use a `0` to start their area codes. It is generally (but not always) dropped when quoting a number in international format, as international callers wouldn't dial it. – Spudley Sep 25 '11 at 20:52
  • Right, but what I'm saying is that I probably would not put that format on my website as its difficult to read, I would break it up.. no? – Rabbott Sep 25 '11 at 20:58
  • 1
    sorry. the way I read the question it seemed like you didn't know what formats you would be getting, so I assumed you'd need to cope with anything in common use. – Spudley Sep 25 '11 at 21:04
  • "Because this is for scraping, and I can assume people enter phone numbers in a formatted way, I can expect a space, a ., or a - in between the area code and last 4 digits.." Last sentence of the question.. – Rabbott Sep 25 '11 at 21:05
  • yep, got it now. as I said - 'sorry'. :) – Spudley Sep 25 '11 at 21:11
  • 1
    @Rabbott - Since you seem to have an idea of the different intl phone number formats you expect, you should probably enumerate them in your question, this way, people can give you the sort of answers you want. – ocodo Sep 26 '11 at 00:20
  • @Rabbott - By the way, if you are dealing with a lot of UK numbers, you will find that `(0)` is actually quite common. I've also seen it appear in Australian telephone numbers, among others. Generally it's used where the number is presented to both domestic and international potential callers. – ocodo Sep 26 '11 at 00:23
  • @Rabbott - one other thing.. "and I can assume people enter phone numbers in a formatted way" ... can you expand on this assumption? – ocodo Sep 26 '11 at 00:25
  • @Slomojo No, i dont have an idea of what it should look like. I just know that I had a regex that I thought would work, which I will edit my question to display, but then the (0) showed up and i started wondering how many other random things did I not account for..? As far as formatted, I mean that I wouldn't imagine someone would display their email as a string of digits with no separators (144546787654) because its not easily read.. – Rabbott Sep 26 '11 at 02:49
  • 1
    @Rabbott - I'd first suggest that you consider that you will miss certain edge cases, 144546787654 being a reasonable case, since as you say, it's unlikely to be used. Just be aware that different countries have a variety of traditional ways of formatting phone numbers, for example, in Germany and other European countries it's quite common to format the number +49.372.444.8181. What you need to do is gather a set of acceptable formats and then present those in your question. – ocodo Sep 26 '11 at 03:17
  • 1
    Have a look at this SO question http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Narendra Yadala Sep 26 '11 at 03:41

0 Answers0