Python phonenumber regex doesn't work well enough
I have this regex code that I am using in my code:
pattern = re.compile('\d{3,4}(\/?)(\d{6,6})')
m= pattern.match('0481/987421')
if m:
print "yes"
else:
print "no"
It's a regex that should work for phonenumbers like this: dddd / dddddddd so there are 3 or 4 digits first, then a forward slash or not, and then exactly 6 digits. It works great, for example 21/484135 doesn't work and other wrong things don't work either. But the problem with this regex is that when my first characters are correct and I type something random behind it, it still prints yes. I mean something like this: 0481/9874214879516874 I think because the regex matches the first 11 characters, it returns its matches, no matter what's behind.
How can I solve this problem?
source to share
I would suggest using phonenumbers instead of writing your own regex. Here's an example of parsing a Belgian phone number:
>>> x = phonenumbers.parse("0481/987421", "BE")
>>> x
PhoneNumber(country_code=32,
national_number=481987421L,
extension=None,
italian_leading_zero=False,
country_code_source=None,
preferred_domestic_carrier_code=None)
It throws an exception on invalid phone numbers:
>>> x = phonenumbers.parse("0481/9874214879516874", "BE")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/phonenumbers/phonenumberutil.py", line 2038, in parse
"The string supplied is too long to be a phone number.")
phonenumbers.phonenumberutil.NumberParseException: (4) The string supplied is too long to be a phone number.
source to share