当前位置 博文首页 > IPV4和IPV6正则表达式的深入讲解

    IPV4和IPV6正则表达式的深入讲解

    作者:copbint 时间:2021-08-15 19:01

    IPV4正则表达式

    Ipv4地址分为ABCDE五大类,其中ABC类是普通ip地址,D类是组播地址,E类保留,作为研究之用。

    范围分别为:

    A: 1.0.0.1 ―一126.155.255.255

    内网地址范围:10.0.0.0 一一10-255.255.255

    B: 127.0.0.1 —191.255.255.255

    内网地址范围:172.16.0.0——172.31.255.255

    C: 192.0.0.1 —223.255.255.255

    内网地址范围:192.168.0.0—一192.168.255.255

    D: 224.0.0.1 —239.255.255.255

    E: 240.0.0.1 —255.255.255.255

    我们的正则要求ip必须是ABC类地址。每个字节的第一个数字可以为0,比如说01, 001。

    1.       ip的第一个字节分作以下几种情况:

    1.长度为3且以2开头,范围为200-223

    正则:22[0-3]丨2[0-1][0-9]

    2.长度为3且以0或1开头

    正则:[0-1][0-9][0-9]

    3.长度为1或2

    正则:([0-9])]{1,2}

    所以第一个字节的表达式为:

    (22[0-3]丨2[0-1][0-9]|

    [0-1][0-9][0-9]|

    0[1 -9][0-9]|

    ([0-9])]{1,2})

    2.    后面三个字节范围为0-255,前面有一个点

    分为以下几种情况:

    1.    以2开头

    正则:25[0-5]|2[0-4][0-9]

    2.    以1和0开头的情况和第一个字节相同。

    所以,单个字节的正则表达式:

    ([.]

    (25[0-5]|2[0-4][0-9]|

    [0-1][0-9][0-9]|

    0[1 -9][0-9]|

    ([0-9])]{1,2}))

    3.    加上点号和重复三次,以及开始和结尾匹配符,IPV4最终正则表达式变为:

    ((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})

    点号之所以用中括号括起来是因为如果不扩起来是匹配任意字符。也可以用两个反斜杆转义。

    加上行首和行尾匹配符:

    (^((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})$)

    IPV6正则表达式

    IPV6介绍

    IPV6的长度是128位,相比于ipv4的32位,极大的扩展了ip地址可用空间。ipv4地址现在被视为一种稀缺资源,而ipv6地址相当充足,在可以预见的未来是用不完的。有这样一段描述:如果地球表面(含陆地和水面)都覆盖着计算机,那么IPv6允许每平方米拥有7*10A23个IP地址;如果地址分配的速率是每微秒100万个,那么需要10A19年才能将所有的地址分配完毕。

    IPv6地址表示

    IPv6的128位地址通常写成8组,每组为四个十六进制数的形式。比如:

    AD80:0000:0000:0000:ABAA:0000:00C2:0002

    是一个合法的IPv6地址。这个地址比较长,看起来不方便也不易于书写。零压缩法可以用来缩减其长度。如果几个连续段位的值都是0,那么这些0就可以简单的以::来表示,上述地址就可写成:

    AD80::ABAA:0000:00C2:0002

    这个简化只能用一次,在上例中的ABAA后面的0000就不能再次简化。当然也可以在ABAA后面使用::,这样的话前面的12个0就不能压缩了。这个限制的目的是为了能准确还原被压缩的0,不然就无法确定每个::代表了多少个0。例如,下面是一些合法的IPv6地址:

    CDCD:910A:2222:5498:8475:1111:3900:2020

    1030::C9B4:FF12:48AA:1A2B

    2000:0:0:0:0:0:0:1::

    0:0:0:0:0:0:12000:0:0:0:0::

    同时每个段前面的零可以省略,因此

    2001:0DB8:02de::0e13 等价于

    2001:DB8:2de::e13

    一个IPv6地址可以将一全IPv4地址内嵌进去,写成IPv6形式和平常习惯的IPv4形式的混合体。

    IPv6有两种内嵌IPv4的方式:IPv4映像地址和IPv4兼容地址(已经被舍弃)。

    IPv4映像地址

    0000:0000:0000:0000:0000:ffff:192.168.89.9这种混合写法对应的ipv6地址:

    0000:0000:0000:0000:0000:ffff:c0a8:5909

    其实表示的是192.168.89.9这个ipv4地址。IPv4映像地址布局如下:

    0000…..0000(80bits)| FFFF | IPv4 address |

    IPv4兼容地址

    兼容地址和映像地址的区别就是第81-96位为0。

    IPv4兼容地址布局如下:

    0000…..0000(80bits) | 0000 | IPv4 address |

    格式分为以下几种情况:

    1.    前面7个完整的字段,第8个字段为零压缩写法或者数字。如:

    1:2:3:4:5:6:7:8

    1:2:3:4:5:6:7::

    对应的正则表达式为:

    (([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))

    最外面加一层括号是为了保证与其他正则表达式拼接到一起时保证不产生混乱。

    2.    前面6个完整的字段,后面2个字段可能为零压缩写法或者ipv4地址嵌入式写法。如:

    1:2:3:4:5:6::8

    1:2:3:4:5:6::

    1:2:3:4:5:6:192.168.1.1

    对应的正则表达式为:

    (([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    3.    前面5个完整的字段,后面3个字段可能为零压缩写法或者ipv4地址嵌入式写法。如:

    1:2:3:4:5::

    1:2:3:4:5::6

    1:2:3:4:5::8

    1:2:3:4:5::192.168.1.1

    对应的正则表达式为:

    (([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    4.    前面4个完整的字段,后面4个字段可能为零压缩写法或者ipv4地址嵌入式写法。如:

    1:2:3:4::

    1:2:3:4::5

    1:2:3:4::8

    1:2:3:4::192.168.1.1

    对应的正则表达式为:

    (([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    前面3,2,1个完整字段的情况略。

    8.    第一个字段即开始简略写法

    ::8

    ::192.168.1.1

    对应的正则表达式为:

    (:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    所以IPV6的正则表达式为:

    (([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|

    (([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|

    (:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    上面为了表达逻辑的清晰,用了换行符,删除换行符,便得到IPV6的最终正则表达式:

    (([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))

    加上行首和行尾匹配符:

    (^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))$)

    上面的表达式还有考虑的不完善的地方:兼容地址和映射地址的前80bit一定是0,所以上面的范围其实写的更宽了,而且使表达式变复杂了。但是发现这个问题的时候,已经没有优化的需求了,所以就先这样,有需要的自行进行优化。

    HTTPS

    正则:

    ((http|https|HTTP|HTTPS)://.{1,245})

    加上行首和行尾匹配符:

    (^((http|https|HTTP|HTTPS)://.{1,245})$)

    域名

    对应的规则:

    1.       非最后一段

    a.    字符范围为:a-zA-Z0-9以及短划线-

    b.    开始和结束字符不能为-

    c.     长度不超过63

    2.       最后一段

    a.       字符范围为a-zA-Z

    b.       长度为2-6

    3.    不能只有最后一段

    正则:

    (([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}

    加上行首和行尾匹配符:

    (^(([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}$)

    空字符串

    (^$)

    附IPv4及IPv6正则测试用例——java:

    import org.junit.Test;
    
    public class IPv6Test {
     public static final String ipv4Regex = "(^((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})$)";
     public static final String ipv6Regex = "(^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))$)";
     public static final String httpRegex = "(^((http|https|HTTP|HTTPS)://.{1,245})$)";
     public static final String domainRegex = "(^(([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}$)";
     public static final String emptyRegex = "(^$)";
    
     public static final String finalRegex = ipv4Regex + "|" + ipv6Regex + "|" + httpRegex + "|" + domainRegex + "|" + emptyRegex;
    
     public static void main(String args[]) {
      try {
    
      } catch (Exception e) {
       e.printStackTrace(System.out);
      }
    
     }
    
     // 第一个字段长度为3的测试用例
     @Test
     public void testIpv4_1() {
      assert ("200.255.255.255".matches(finalRegex));
      assert ("223.255.255.255".matches(finalRegex));
      assert ("224.255.255.255".matches(finalRegex));
    
      assert ("192.0.0.1".matches(finalRegex));
      assert ("127.0.0.1".matches(finalRegex));
      assert ("100.0.0.1".matches(finalRegex));
      assert ("090.0.0.1".matches(finalRegex));
      assert ("009.0.0.1".matches(finalRegex));
    
     }
    
     // 第一个字段长度为1或2的测试用例
     @Test
     public void testIpv4_2() {
      assert ("09.255.255.255".matches(finalRegex));
      assert ("90.255.255.255".matches(finalRegex));
      assert ("00.255.255.255".matches(finalRegex));
    
      assert (!"-.0.0.1".matches(finalRegex));
      assert ("0.0.0.1".matches(finalRegex));
      assert ("1.0.0.1".matches(finalRegex));
     }
    
     // 测试后面三个字节
     @Test
     public void testIpv4_3() {
      assert ("200.0.255.255".matches(finalRegex));
      assert ("200.01.255.255".matches(finalRegex));
      assert ("200.10.255.255".matches(finalRegex));
      assert (!"200.256.255.255".matches(finalRegex));
      assert ("200.001.255.255".matches(finalRegex));
    
      assert ("200.255.0.255".matches(finalRegex));
      assert ("200.255.01.255".matches(finalRegex));
      assert ("200.255.10.255".matches(finalRegex));
      assert (!"200.255.256.255".matches(finalRegex));
      assert ("200.255.001.255".matches(finalRegex));
    
      assert ("200.255.255.0".matches(finalRegex));
      assert ("200.255.255.01".matches(finalRegex));
      assert ("200.255.255.10".matches(finalRegex));
      assert (!"200.255.255.256".matches(finalRegex));
      assert ("200.255.255.001".matches(finalRegex));
    
     }
    
     // 测试异常
     @Test
     public void testIpv4_4() {
      assert (!"200".matches(finalRegex));
      assert (!"200.1".matches(finalRegex));
      assert (!"200.1".matches(finalRegex));
      assert (!"200.1.1".matches(finalRegex));
      assert (!"200.1.1.1.1".matches(finalRegex));
     }
    
     @Test
     public void testIpv6_1() {
      assert ("1:2:3:4:5:6:7::".matches(finalRegex));
      assert ("1:2:3:4:5:6:7:8".matches(finalRegex));
    
      assert ("1:2:3:4:5:6::".matches(finalRegex));
      assert ("1:2:3:4:5:6::8".matches(finalRegex));
    
      assert ("1:2:3:4:5::".matches(finalRegex));
      assert ("1:2:3:4:5::8".matches(finalRegex));
    
      assert ("1:2:3:4::".matches(finalRegex));
      assert ("1:2:3:4::8".matches(finalRegex));
    
      assert ("1:2:3::".matches(finalRegex));
      assert ("1:2:3::8".matches(finalRegex));
    
      assert ("1:2::".matches(finalRegex));
      assert ("1:2::8".matches(finalRegex));
    
      assert ("1::".matches(finalRegex));
      assert ("1::8".matches(finalRegex));
    
      assert ("::".matches(finalRegex));
      assert ("::8".matches(finalRegex));
      assert ("::7:8".matches(finalRegex));
      assert ("::6:7:8".matches(finalRegex));
      assert ("::5:6:7:8".matches(finalRegex));
      assert ("::4:5:6:7:8".matches(finalRegex));
      assert ("::3:4:5:6:7:8".matches(finalRegex));
      assert ("::2:3:4:5:6:7:8".matches(finalRegex));
    
      assert ("::192.168.1.1".matches(finalRegex));
    
     }
    
     @Test
     public void testIpv6_2() {
      assert ("A:0f:0F:FFFF:5:6:7:8".matches(finalRegex));
      assert (!"A:0f:0F:FFFF1:5:6:7:8".matches(finalRegex));
      assert (!"G:0f:0F:FFFF:5:6:7:8".matches(finalRegex));
     }
    
     @Test
     public void testHttp() {
      assert ("https://a.com".matches(finalRegex));
      assert ("https://a.b.c.com".matches(finalRegex));
      assert ("https://a".matches(finalRegex));
      assert ("https://a.comdddd".matches(finalRegex));
      assert (!"https://afadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfaafadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfaafadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfa.comdddd"
        .matches(finalRegex));
     }
    
     @Test
     public void testDomain() {
      assert ("a.com".matches(finalRegex));
      assert ("a.bdfad-dfadf.c.com".matches(finalRegex));
      assert (!"a.-bdfad-dfadf.c.com".matches(finalRegex));
      assert ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijk.com".matches(finalRegex));
      assert (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijk1.com".matches(finalRegex));
     }
    
     @Test
     public void testEmpty() {
      assert ("".matches(finalRegex));
      assert (!"1".matches(finalRegex));
    
     }
    }
    
    jsjbwy
    下一篇:没有了