Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to parse a phone number

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to parse a phoneNumber in order take out country code, area code etc.
What will be the best way to do that...

e.g i have number "+919889988998"
or "+9889988998"
there can be many more cases.

 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the format is different for each country, you'd need to have rules somewhere for every country you wish to support.

Also, the examples you mention show a further complication: The "+" sign is generally used to mean that a country code follows - but that's only the case with the first number, not the second. You also need to account for the fact that numbers can differ depending on whether they are intended to be called from within a country, or from outside of a country.
 
Marshal
Posts: 70696
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably search and find regular expressions which other people have used in the past for phone numbers. But as Ulf says, it is country-specific, and in Britain region-specific. London phone numbers have a different format from Manchester phone numbers which have a different format from Leeds phone numbers and they have a different format from Cambridge phone numbers and companies tend to have a different format etc etc
 
Nrapendra Sharma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to detect country mainly.
is there any standard implementation which can be refered for creating a phone parser ..??

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nrapendra Sharma wrote:i need to detect country mainly.
is there any standard implementation which can be refered for creating a phone parser ..??


I think you could use a list of dialing codes - http://en.wikipedia.org/wiki/List_of_country_calling_codes

See how the codes are not overlapping. eg. if Egypt is +20, there's no country with +20x

So you could put these codes in a map (code -> country), then check the first digit. If that matches a code, then that's the country. If it does not, take the second digit to get a two digit code and see if that's in the map. I don't think that's most elegant, but maybe better than getting a list of regular expressions if all you're trying to do is detect country.
 
That is a really big piece of pie for such a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic