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"
    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

2 answers

You need to bind your expression. Add $

or \Z

at the end to make sure nothing follows. You can also add ^

to bind it at the beginning of the line, but this is not required when using with match()


pattern = re.compile(r"^\d{3,4}/?\d{6}\Z")




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


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/", 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.




All Articles