Regexp for german phone number format

14,956

Solution 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

Solution 2

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)

Solution 3

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

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

May introduce some false positives.

Share:
14,956

Related videos on Youtube

Mann87
Author by

Mann87

Updated on September 15, 2022

Comments

  • Mann87
    Mann87 over 1 year

    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.

  • Willi Mentzel
    Willi Mentzel over 5 years
    the second minus will be interpreted as "until". you should mask it to avoid unwated matches. (\(?([\d \-\)\–\+\/\(]+){6,}\)?([ .\-–\/]?)([\d]+)) we noticed it when we used it :D. good answer
  • Toto
    Toto over 3 years
    Why do you escape all characters in the character class? Why do you put twice the hyphen? That makes your regex unreadable.Moreover a double quantifier + & {6,} in ([\d \-\)\–\+\/\(]+){6,} doesn't make sense.
  • dewey
    dewey over 2 years
    Be careful this validation also matches +-+++49 (173) 1799 806-44