Python phonenumber regex doesn't work good enough

巧了我就是萌 提交于 2019-12-23 18:00:05

问题


I have this regex code I use 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 first 3 or 4 digits, then a slash or not and then exactly 6 digits. It works fine, for example 21/484135 doesn't work and other wrong things also don't work. But the problem of this regex is, when my first characters are right and I type anything random behind it it would still print "yes". I mean something like this: 0481/9874214879516874 I think because the regex matches for the first 11 characters it returns it matches and it doesn't matter what comes behind it.

How can I solve this problem?


回答1:


You need to anchor your expression. Add a $ or \Z at the end of it to make sure nothing follows. You could also add ^ to anchor it at the beginning of string, altho that is not required when used with match().

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



回答2:


I would suggest using the phonenumbers module instead of writing your own regular expression. 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 will throw 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.


来源:https://stackoverflow.com/questions/9711664/python-phonenumber-regex-doesnt-work-good-enough

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!