IPV4和IPV6正则表达式的深入讲解
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)); } }
到此这篇关于ipv4和ipv6正则表达式的文章就介绍到这了,更多相关ipv4和ipv6正则表达式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!