加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 产品 > 正文

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

发布时间:2020-12-15 16:04:54 所属栏目:产品 来源:互联网
导读:这篇文章主要给大家介绍了关于IPV4和IPV6正则表达式的相关资料,文中介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来

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

}

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读