Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression to match IPV4 address doesn't work

Tags:

python

regex

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?

like image 901
ManStudent Avatar asked Jan 10 '23 05:01

ManStudent


1 Answers

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.

like image 146
Martijn Pieters Avatar answered Jan 26 '23 02:01

Martijn Pieters