Link to home
Start Free TrialLog in
Avatar of jlalande
jlalande

asked on

Regular expression to match phone numbers in Germany

I have an Office Smart Tag that uses a regular expression to match phone numbers that are in North American format. I have a user in Germany that would like me to update the smart tag to recognize German phone numbers.

The user has given me the following phone numbers that the smart tag does not currently recognize, but that should after updating:

(06442)  3933023    
(02852) 5996-0      
(042) 1818 87 9919  
06442 / 3893023      
06442/3839023        
042/ 88 17 890 0    
+49 221 549144 – 79  
+49 221 - 542194 79  
+49 (221) - 542944 79
0 52 22 - 9 50 93 10
+49(0)121-79536 - 77
+49(0)2221-39938-113
+49 (0) 1739 906-44  
+49 (173) 1799 806-44
+44(0)1731234567    

In ASP.NET, there is a RegularExpressionValidator and its German phone number regex is ((\(0\d\d\) |(\(0\d{3}\) )?\d )?\d\d \d\d \d\d|\(0\d{4}\) \d \d\d-\d\d?), however it doesn't match any of those numbers. That isn't surprising given the comments about calling in Germany at this site: http://www.justlanded.com/english/Germany/Germany-Guide/Telephone-Internet/Calling.

Are there any regex experts that can lend a hand on this?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlalande
jlalande

ASKER

Thanks for your help kaufmed.

Unfortunately, once compiled into the smart tag, the only number recognized was +44(0)1731234567. Others were chopped in two where there is a space or a '/', or the 'extension' was left off.

I get the impression that this is going to be a major PITA.
Examples of what should be accepted aren't always the best way to write a regular expression; we really need some direction on what *shouldn't* be accepted. Even better would be some guidance on the general formats.

eg, it might be all of the following:
+49 then a 0 or 1, then 12 digits
+49 then a 2, then 11 more digits
0, then 2 or 4 or 5 or 6, then 9 or 10 more digits

That way we can provide a pattern that rejects numbers with unexpected prefixes or numbers of digits. This is important if you want good data.
My mistake. I didn't place the regex in the correct location before compiling. Correcting my mistake and testing shows that it matches all of the listed strings.

Thanks!