Reputation: 495
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?
Upvotes: 3
Views: 1067
Reputation: 67083
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.
Upvotes: 2
Reputation: 33918
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")
Upvotes: 4