Regular expression for dividing country calling codes

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-04-07T12:00:41Z Indexed on 2010/04/07 12:03 UTC
Read the original article Hit count: 256

Filed under:

Hi

I have a list of calling codes for all countries(the phone number prefixes), I would like to split them up in the country name and the actual code so I can put then into an xml.

I have tried back and forth but can not get a regexp going that takes all cases into account. I think it is fairly simple for someone with a bit of experience.

The codes have these formats:

Afghanistan 93
Anguilla 1 264
Antarctica 6721
Antigua and Barbuda 1 268
Bosnia and Herzegovina 387
Canada 1
Congo, Republic of the 242
Cote d'Ivoire 225
Ireland (Eire) 353
United States of America 1

There are around 235 of them in total, but these are the regulars and the exceptions.

^[a-zA-Z]\s,'()] for between 1 and X words and then it is [0-9\s]{1,5}$ for the numbers:

X
XX
XXX
XXXX
X XXX 

So if I should express it as a sentence it would be: "from beginning of a line, take all characters (1) including space,'() until you encounter digits, then take all of these including space(2) until you encounter a line break."

I am using TextMate, and the docs says:

TextMate uses the Oniguruma regular expression library by K. Kosako.

I would appreciate any help given:) Thank you.

© Stack Overflow or respective owner

Related posts about regex