I need a Python regex which matches to mobile phone numbers from Germany and Austria.
In order to do so, we first have to understand the structure of a phone number:
- a mobile number can be written with a country calling code in the beginning. However, this code is optional!
- if we use the country calling code the trunk prefix is redundant!
- The prefix is composed out of the trunk prefix and the company code
- The prefix is followed by an individual and unique number with 7 or 8 digits, respectivley.
List of German prefixes:
- 0151, 0160, 0170, 0171, 0175, 0152, 0162, 0172, 0173, 0174, 0155, 0157, 0159, 0163, 0176, 0177, 0178, 0179, 0164, 0168, 0169
List of Austrian prefixes:
- 0664, 0680, 0688, 0681, 0699, 0664, 0667, 0650, 0678, 0650, 0677, 0676, 0660, 0699, 0690, 0665, 0686, 0670
Now that we know all rules to build a regex, we have to consider, that humans sometimes write numbers in a very strange ways with multiple whitespaces, /
or ()
. For example:
- 0176 98 600 18 9
- +49 17698600189
- +(49) 17698600189
- 0176/98600189
- 0176 / 98600189
- many more ways to write the same number
I am looking for a Python regex which can match all Austian and German mobile numbers.
What I have so far is this:
^(?:\+4[39]|004[39]|0|\+\(49\)|\(\+49\))\s?(?=(?:[^\d\n]*\d){10,11}(?!\d))(\()?[19][1567]\d{1,2}(?(1)\))\s?\d(?:[ /-]?\d)+