IPv6IP字符串格式的正则表达式
1、表达式全文
/^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{0,4}:):([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{1,4}:){4}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{1,4}:){5}:((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^[\da-fA-F]{0,4}:((:[\da-fA-F]{1,4}){1,6}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){5}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){6}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$/
2、分段解释
2.1 IPv6 兼容IPv4规则
IPv6兼容IPv4的字符串格式是一种特殊的IPv6地址表示方法,用于在IPv6网络中表示IPv4地址。这种表示方法使用了IPv4映射地址和6to4地址。
IPv4映射地址用于将IPv4地址嵌入到IPv6地址中。它使用冒号(:)分隔的四个十六进制数字来表示,其中前三个数字表示网络的IPv6前缀,而后一个数字表示IPv4地址的八位数。例如,0:0:0:0:0:FFFF:w.x.y.z 或 ::FFFF:w.x.y.z 表示一个IPv4地址 w.x.y.z。
2.1.1 IPv4的正则
IPv4地址位小于等于255的三个数字用小数点分割。例如,w.x.y.z 表示一个IPv4地址 w.x.y.z。
/^((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$/
2.1.2 IPv6兼容IPv4全字符的正则
IPv6兼容IPv4的字符串格式,则是在IPv4串前加上以冒号隔开的6组4位16进制字符,并在IPv4前加冒号隔开。例如,FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:w.x.y.z 。
^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$
2.1.3 IPv6兼容IPv4字符带省略写法的正则
IPv6格式可以进行缩写,缩写规则是两个冒号之间如果都是0的话可以省略,出现一个双冒号,但是双冒号只能出现一个,例如,FFFF::FFFF:FFFF:FFFF:FFFF:ww.x.y.z 或 ::FFFF:w.x.y.z
^([\da-fA-F]{0,4}:):([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|
^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|
^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|
^([\da-fA-F]{1,4}:){4}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|
^([\da-fA-F]{1,4}:){5}:((25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(25[0-5]|2[0-4]\d|1?\d\d?)$|
2.2 IPv6字符串格式
IPv6地址由8组由冒号分隔的4位十六进制数字组成,每组数字范围在0到ffff之间。例如,FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF。
IPv6格式可以进行缩写,缩写规则是两个冒号之间如果都是0的话可以省略,出现一个双冒号,但是双冒号只能出现一个,例如,FFFF::FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 或 ::FFFF
2.2.1 IPv6全字符串的正则
IPv6地址由8组由冒号分隔的4位十六进制数字组成,每组数字范围在0到ffff之间。例如,FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF。文章来源:https://www.toymoban.com/news/detail-845560.html
^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$
2.1.2 IPv6字符带省略写法的正则
IPv6格式可以进行缩写,缩写规则是两个冒号之间如果都是0的话可以省略,出现一个双冒号,但是双冒号只能出现一个,例如,FFFF::FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 或 ::FFFF文章来源地址https://www.toymoban.com/news/detail-845560.html
^[\da-fA-F]{0,4}:((:[\da-fA-F]{1,4}){1,6}|:)$|
^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,5}|:)$|
^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,4}|:)$|
^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,3}|:)$|
^([\da-fA-F]{1,4}:){5}((:[\da-fA-F]{1,4}){1,2}|:)$|
^([\da-fA-F]{1,4}:){6}:([\da-fA-F]{1,4})?$|
^([\da-fA-F]{1,4}:){6}:$
到了这里,关于【IPv6】IP字符串格式的正则表达式及详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!