Regexp for german phone number format

安稳与你 提交于 2019-12-19 21:21:41

问题


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.


回答1:


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




回答2:


[0-9]*\/*(\+49)*[ ]*(\([0-9]+\))*([ ]*(-|–)*[ ]*[0-9]+)*

Check this link: https://regex101.com/r/CAVex8/1

May introduce some false positives.



来源:https://stackoverflow.com/questions/41538589/regexp-for-german-phone-number-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!