Possible Duplicate:
Regular expression to match phone numbers with country codes
Currently I have this regex do validate/capture phone numbers: ^(\+?(?<country>\d{1,3}?) ?)?(\(?0?(?<area>\d{2})\)? ?)?(?<phone>9?\d{4}[-. ]?\d{4})$
It accepts numbers like 88888888, 8888-8888, 8888.8888, 11 8888-8888, (11) 88888888, +22 (21) 8888-8888 and every combination in this format.
The problem is: when I pass the area code without contry code (like this: (11) 8888-8888 or 11 8888-8888 or even 1188888888) it gets the area code part (11) as the country code, and leaves the area code empty.
How could I do something like country + area + phone ORarea + phone but NOT country + phone? Without doing something like this:
^(((\(?0?(?<area>\d{2})\)? ?)?|(\+?(?<country>\d{1,3}?) ?)?(\(?0?(?<area>\d{2})\)? ?)?)(?<phone>9?\d{4}[-. ]?\d{4})$
Wich is (area+phone|country+area+phone) but the area and the country regexp gets repeated twice.
In fact there is an ambiguity between a country code with two digits and an area code with two digits. When the regexp hits this case, it considers the area code as a country code since country code is placed first and area code is optional.
SOLUTION #1 : Using a negative lookahead (?!…)
To solve this problem, one could tell the regexp to ignore two digits country code followed by a phone number. In order to keep the regexp small enough (thus not too complicated), it’s possible to give just the beginning of a phone number.
Here is the negative lookahead to use:
(?!9?\d{4})A two digits number is qualified as a country code ONLY if it is not followed by the start of a phone number (
9?\d{4}).The final regexp is :
SOLUTION #2: Templating the regular expression
IMO, if a flavor could allow to build a regular expression with templates, it would be a powerful feature. Well this feature doesn’t exist in any flavor I know of.
But there is a workaround. The following code is a snippet written in C#