Regexp for german phone number format
Asked Answered
R

6

9

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.

Rabon answered 8/1, 2017 at 22:28 Comment(0)
W
14

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

Wellknit answered 19/12, 2018 at 15:18 Comment(4)
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 answerAlbert
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.Berger
Be careful this validation also matches +-+++49 (173) 1799 806-44Angarsk
According to DIN 5008, the following Regex can be used: /(((\+49 ))|(0))\d{3,4} (\d{7}|\d{6}(-\d{2})?)/gGoggles
H
1
[0-9]*\/*(\+49)*[ ]*(\([0-9]+\))*([ ]*(-|–)*[ ]*[0-9]+)*

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

May introduce some false positives.

Harem answered 8/1, 2017 at 23:6 Comment(0)
T
1

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)

Tailpiece answered 28/12, 2020 at 12:9 Comment(0)
P
1

This is no solution for the asked question, just an advice for matching phonenumbers!

If you are about to store telephone numbers for you first time, then limit the amount of different accepted formats. Get rid of these for example:

(06442) 3933023
042/ 88 17 890 0
+49(0)121-79536 - 77
02162 - 54 91 44 79

Why? You need to test more possible ways of inputting an invalid value.

Those formats you absolutely need to concider according to DIN 5008:

0873 376461
03748 37682358
05444 347687-350
0764 812632-41
0180 2 12334
0800 5 23234213
+49 30 3432622-113
0179 1111111

Here is what I came up with: Regex

^(([+]{1}[1-9]{1}[0-9]{0,2}[ ]{1}([1-9]{1}[0-9]{1,4}){1}[ ]{1}([1-9]{1}[0-9]{2,6}){1}([ -][0-9]{1,5})?)|([0]{1}[1-9]{1}[0-9]{1,4}[ ]{1}[0-9]{1,8}([ -][0-9]{1,8})?)?)

Positives:

06429 1111
06901 306180
06429 231
0800 3301000
0179 1111111
0873 376461
03748 37682358
05444 347687-350
0764 812632-41
0180 2 12334
0800 5 23234213
+49 6429 1111
+49 39857 2530
+55 11 2666-0054
+300 11 2666-0054
+49 641 20106 0
+49 641 20106
+49 30 3432622-113

Negatives:

++49 157 184977
+300 11 0000-0000
(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 - 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
01517953677
+491517953677
015777953677
02162 - 54 91 44 79
(02162) 54 91 44 79
saddsadasdasd
asdasd
asdasd asdasd asd
asdasd
kjn asohas  asdoiasd
23434 234 234 23
323
23434 234----234
///// ----
// id8834 3493934 //
Proudfoot answered 11/11, 2021 at 12:58 Comment(0)
K
0

Hey i have a little enhancement for despecial‘s Regex:

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

It filters numbers that have too high occurrencies of /

Kirtle answered 14/2, 2022 at 23:7 Comment(0)
G
0

The german phone numbers should be displayed in DIN 5008 format, as written here: https://www.placetel.de/blog/telefonnummer-schreibweise-richtig-schreiben#1.Die_richtige_Schreibweise_von_deutschen_Telefonnummern(nach_DIN_5008)

I created following Regex which I am actively using:

/(((\+49 ))|(0))\d{3,4} (\d{7}|\d{6}(-\d{2})?)/g

see https://regex101.com/r/l6p3sH/1

Additional to that, I set the maximum length to 10 with JavaScript form validation.

Goggles answered 18/6 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.