Python 验证IPv6地址

本文提供了一系列针对IPv6地址的正则表达式,包括标准记法、混合记法及压缩记法,适用于验证和从大量文本中查找IPv6地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值