简体   繁体   中英

Regex: How to match IPv6 address ending in ::\d+ (Python)?

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.

I would suggest to avoid using RegEx wherever you can, because implemting this manually often leads to errors (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM