Reputation: 16189
The regex given here for IPv6 addresses works well for most IPv6 address I am looking to extract.
But it falls short with address 2607:f8b0:4001:c03::247 (mail-ie0-x247.google.com), matching only up to and including the last two colons thus 2607:f8b0:4001:c03::
How would we extend the regex (for Python) such that it catches cases like this (without breaking it for other variants? I'm not sure how Google sets up its IPv6 addresses, but I guess it's conceivable that the characters coming after the last two colons could be a mixture of both letters and numbers, so the regex should handle this also.
Upvotes: 0
Views: 1584
Reputation: 11002
I would suggest to avoid using RegEx wherever you can, because implemting this manually often leads to errors (e.g. you can not check if this RegEx actually works for every possible IPv6 address).
Some better solution (in my opinion) is using the python built-in ip adress manipulation library:
import ipaddress
sample_addr = "2001:db8::"
try:
# executed if (and only if) ip address is valid
ipaddress.ip_address(sample_addr)
except ValueError:
print "address invalid"
# further error handling
ip_address()
returns an IPv6 or IPv4 object (in dependence of the input) or raises a ValueError if the input was non-valid.
Upvotes: 4