1. 程式人生 > >Python 驗證IPv6地址

Python 驗證IPv6地址

1. Standard notation
eg.
1763:0:0:0:0:b03:1:af18 

1.1 check whether the whole subject text is an IPv6 address using standard notation
^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$


1.2 Find an IPv6 address using standard notation within a larger collection of text
(?<![:.\w])(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}(?![:.\w])


2. Mixed notation
eg.
1763:0:0:0:0:b03:127.32.67.15

2.1 Check whether the whole subject text is an IPv6 address using mixed notation
^(?:[a-fA-F0-9]{1,4}:){6}(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$

2.2 Find IPv6 address using mixed notation within a larger collection of text
(?<![:.\w])(?:[a-fA-F0-9]{1,4}:){6}(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?![:.\w])

3. Compressed notation
eg.
1762::B03:1:AF18

3.1 Check whether the whole subject text is an IPv6 address using standard or compressed notation
(?x)\A(?:
# Standard
(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}
# Compressed with at most 7colons
|(?=(?:[a-fA-F0-9]{0,4}:){0,7}[a-fA-F0-9]{0,4}
\Z) # and anchored
# and at most 1 double colon
(([0-9a-fA-F]{1,4}:){1,7}|:)((:[0-9a-fA-F]{1,4}){1,7}|:)
# compressed with 8 colons
|(?:[a-fA-F0-9]{1,4}:){7}:|:(:[a-fA-F0-9]{1,4]){7}
)\Z

3.2 Find IPv6 address using standard or compressed notation
(?x)(?<![:.\w])(?:
# Standard
(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}
# Compressed with at most 7colons
|(?=(?:[a-fA-F0-9]{0,4}:){0,7}[a-fA-F0-9]{0,4}
\Z) # and anchored
# and at most 1 double colon
(([0-9a-fA-F]{1,4}:){1,7}|:)((:[0-9a-fA-F]{1,4}){1,7}|:)
# compressed with 8 colons
|(?:[a-fA-F0-9]{1,4}:){7}:|:(:[a-fA-F0-9]{1,4]){7}
)(?![:.\w])