Regexp for german phone number format

Mann87 picture Mann87 · Jan 8, 2017 · Viewed 9.6k times · Source

I try to get phone numbers from string in german format. But I don't get it to full run. The input text is a full HTML-Page with lots of content, not only the numbers.

Possible Formats:

(06442) 3933023     
(02852) 5996-0       
(042) 1818 87 9919   
06442 / 3893023  
06442 / 38 93 02 3     
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
0173173990644
0214154914479
02141 54 91 44 79
01517953677
+491517953677
015777953677
02162 - 54 91 44 79
(02162) 54 91 44 79

I have tried:

$regex =  '~(?:\+?49|0)(?:\s*\d{3}){2}\s*\d{4,10}~';
if(preg_match_all($regex, $input_imprint , $matches)){
    print_r($matches);
}

But it doesn't match only a few formats. I have no idea to do it.

Answer

despecial picture despecial · Dec 19, 2018

Here is a regex to match all your formats. I would suggest then to replace all unwanted characters and you got your desired result.

(\(?([\d \-\)\–\+\/\(]+)\)?([ .\-–\/]?)([\d]+))

If you need a minimum length to match your numbers, use this:

(\(?([\d \-\)\–\+\/\(]+){6,}\)?([ .\-–\/]?)([\d]+))

https://regex101.com/r/CAVex8/143

updated, thanks for the suggestion @Willi Mentzel