I'm using the re
library.
def validate_ip(self, ip):
pattern = re.compile(r'([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])\.{4}')
matchObj = re.match(pattern, ip)
if matchObj == None:
print "Invalid IP:", ip
sys.exit(0)
When I pass the IP 192.0.0.0, the output is:
Invalid IP: 192.0.0.0
Why is it not matching?
Your pattern matches one 3-digit number, followed by exactly 4 dots:
>>> pattern = re.compile(r'([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])\.{4}')
>>> pattern.match('255....')
<_sre.SRE_Match object at 0x1026eda80>
The {4}
doesn't apply to everything preceding it; it only applies to just the \.
.
You want this instead:
r'(([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])\.){3}([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])'
This matches your number pattern plus a .
dot 3 times, because now the {3}
applies to everything in the preceding grouped expression (using (...)
). You then still need to match the last digit group separately.
Demo:
>>> pattern = re.compile(r'(([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])\.){3}([01]?[0-9]?[0-9]|2[0-4][0-9]|2[5][0-5])')
>>> pattern.match('192.0.0.0')
<_sre.SRE_Match object at 0x1023bf588>
As an aside, just use if not match:
to test for a match failure; None
is a false value in a boolean context. Even if you really wanted to test for None
, you should be using if match is None:
, using an identity test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With