How to parse a phone number

There are tons of different writings for a phone number. The simplest one is the spoke one. A list of numbers that can identify several targets in the world.
It is not unique because of the country indicator that is used for disambiguation, what we call the “international format”. Even after that, behind a phone number can be hidden several phones thanks to the extension.

To all of to who think the phone format is a quite conventionnal one, how would your program read this one ?
+1 400-CALL-0ME-NOW
or this one ?
49 345 67890
or this one ?
(0) 33 1 400 500 600,700

Google made an attempt to parse a phone number accurately thanks to the phone number util library. Among others, it is able to recognize any phone number format, to guess which operator it is, and to isolate the country prefix from the rest.

Trouble exists when you want to record the region indicator as well. It does not exist in every country and it is a tough work when the input is all in one block (no space).

I spent several hours to parse the number in a reliable way. And here is the result :

Every input will get split into 4 parts, whatever the complexity of the format is :

country prefix, region prefix, trunk, extension.

Notice that the mobile prefix won’t be included in the region prefix, it usually identifies a device rather than a place.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.