Tutorial :Regular expression for dividing country calling codes



Question:

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.


Solution:1

This posix regex should be sufficient: ^[a-zA-Z ]+[0-9 ]+$


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »