Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp for german phone number format

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.

like image 843
Mann87 Avatar asked Jan 08 '17 22:01

Mann87


People also ask

What is mobile number validation?

Phone number validation is the process of checking if a phone number is accurate. It lets you find out if the phone number you have for a business contact or customer is active and able to receive calls.


3 Answers

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

like image 175
despecial Avatar answered Oct 06 '22 21:10

despecial


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

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

May introduce some false positives.

like image 22
Kakul Chandra Avatar answered Oct 06 '22 19:10

Kakul Chandra


This one solved my problem (extracting phone numers from emails):

r"\+?[0-9]+([0-9]|\/|\(|\)|\-| ){10,}"

A plus sign optional at the front, followed by at least 1 number, followed by at least 10 numbers or delimiting characters such as /, (, ) or - or a space. (There is no official "smallest number of digits" for a telephone number, but I assume they are all at least 11 digits long)

I'm adding this because @Kakul 's solution matched any lien of my text, and using @despecial 's my code would not terminate. (I am guessing it is too computationally expensive for my pc)

like image 1
Jessica Winter Avatar answered Oct 06 '22 20:10

Jessica Winter