Python regular expression to match IPV4 address doesn't work

ManStudent

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?

Martijn Pieters

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python regular expression to match IPV4 address doesn't work

From Dev

Regular expression for IPV4 address validation

From Dev

Regular expression to validate IPv4 address in ruby

From Dev

Doesn't work regular expression

From Dev

Regular expression doesn't work

From Dev

Doesn't work regular expression

From Dev

Regular expression (Python) - match MAC address

From Dev

Why my regular expression doesn't match this?

From Dev

Regular Expression doesn't Match with string

From Dev

Java Regular Expression doesn't find a match

From Dev

Regular Expression Negate Colon doesn't work

From Dev

Java: Regular expression doesn't work as expected

From Dev

Regular Expression doesn't work out

From Dev

mgo regular expression doesn't work

From Dev

Why this Regular Expression doesn't work?

From Dev

a regular expression with except word doesn't work

From Dev

Regular Expression doesn't work in Firefox

From Dev

android regular expression to match an address

From Dev

Regex: How to match IP address in RFC1918 private IPV4 address ranges (in Python)?

From Dev

PHP Regular Expression with IPv4

From Dev

Python Regular Expression not match

From Dev

Regular expression match in python

From Java

Regular expression to match a line that doesn't contain a word

From Dev

Regular expression for word that doesn't match a list of words

From Dev

Regular expression with named subpattern doesn't see the best match

From Dev

Match line that doesn't end with >\s* using regular expression

From Dev

Regular Expression to match string which doesn't contain substring

From Dev

Regular expression to match text that "doesn't" contain a word?

From Dev

Why doesn't regular expression alternation (A|B) match as per doc?

Related Related

  1. 1

    Python regular expression to match IPV4 address doesn't work

  2. 2

    Regular expression for IPV4 address validation

  3. 3

    Regular expression to validate IPv4 address in ruby

  4. 4

    Doesn't work regular expression

  5. 5

    Regular expression doesn't work

  6. 6

    Doesn't work regular expression

  7. 7

    Regular expression (Python) - match MAC address

  8. 8

    Why my regular expression doesn't match this?

  9. 9

    Regular Expression doesn't Match with string

  10. 10

    Java Regular Expression doesn't find a match

  11. 11

    Regular Expression Negate Colon doesn't work

  12. 12

    Java: Regular expression doesn't work as expected

  13. 13

    Regular Expression doesn't work out

  14. 14

    mgo regular expression doesn't work

  15. 15

    Why this Regular Expression doesn't work?

  16. 16

    a regular expression with except word doesn't work

  17. 17

    Regular Expression doesn't work in Firefox

  18. 18

    android regular expression to match an address

  19. 19

    Regex: How to match IP address in RFC1918 private IPV4 address ranges (in Python)?

  20. 20

    PHP Regular Expression with IPv4

  21. 21

    Python Regular Expression not match

  22. 22

    Regular expression match in python

  23. 23

    Regular expression to match a line that doesn't contain a word

  24. 24

    Regular expression for word that doesn't match a list of words

  25. 25

    Regular expression with named subpattern doesn't see the best match

  26. 26

    Match line that doesn't end with >\s* using regular expression

  27. 27

    Regular Expression to match string which doesn't contain substring

  28. 28

    Regular expression to match text that "doesn't" contain a word?

  29. 29

    Why doesn't regular expression alternation (A|B) match as per doc?

HotTag

Archive