我使用IPAddressUtil.isIPv6LiteralAddress (ipAddress)方法来验证IPv6,但是对于IPV6的IPV6 6地址/前缀长度格式(格式在RFC 4291第2.3节中提到),这种方法失败了。
有谁知道任何验证“ipv6 6地址/前缀长度”格式的验证器吗?
IPV6的法律表述
非IPV6的法律表述
发布于 2011-05-11 12:14:11
看看这是否有效:
try {
if (subjectString.matches(
"(?ix)\\A(?: # Anchor address\n" +
" (?: # Mixed\n" +
" (?:[A-F0-9]{1,4}:){6} # Non-compressed\n" +
" |(?=(?:[A-F0-9]{0,4}:){2,6} # Compressed with 2 to 6 colons\n" +
" (?:[0-9]{1,3}\\.){3}[0-9]{1,3} # and 4 bytes\n" +
" \\z) # and anchored\n" +
" (([0-9A-F]{1,4}:){1,5}|:)((:[0-9A-F]{1,4}){1,5}:|:) # and at most 1 double colon\n" +
" |::(?:[A-F0-9]{1,4}:){5} # Compressed with 7 colons and 5 numbers\n" +
" )\n" +
" (?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3} # 255.255.255.\n" +
" (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]) # 255\n" +
"| # Standard\n" +
" (?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4} # Standard\n" +
"| # Compressed\n" +
" (?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4} # Compressed with at most 7 colons\n" +
" \\z) # and anchored\n" +
" (([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:) # and at most 1 double colon\n" +
"|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7} # Compressed with 8 colons\n" +
")/[A-F0-9]{0,4}\\z # Anchor address"))
{
// String matched entirely
} else {
// Match attempt failed
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}大约一年前,我为一些复杂的正则表达式购买了一个名为RegexMagic的非常有用的程序。
这应该是Java,所以它应该编译,我假设/60可以在0000和FFFF之间,您可以修改最后一部分。
/A-F0-9{0,4}是我添加到正则表达式中以匹配示例的内容。
发布于 2013-01-18 00:21:05
您可以使用Guava库,特别是使用com.google.common.net.InetAddresses类,调用isInetAddress()。
isInetAddress
public static boolean isInetAddress(String ipString)
如果提供的字符串是有效的IP字符串文本,则返回true,否则返回false。
参数:作为IP字符串文本计算的ipString - String
如果参数是有效的IP字符串文本,返回:true
发布于 2019-04-07 19:29:05
IPAddress Java库支持以多态方式解析IPv4和IPv6 CIDR子网(即地址/前缀格式)。免责声明:我是项目经理。
下面的方法是验证的示例代码:
static void parse(String str) {
IPAddressString addrString = new IPAddressString(str);
try {
IPAddress addr = addrString.toAddress();
IPAddress hostAddr = addrString.toHostAddress();
Integer prefix = addr.getNetworkPrefixLength();
if(prefix == null) {
System.out.println(addr + " has no prefix length");
} else {
System.out.println(addr + " has host address " + hostAddr + " and prefix length " + prefix);
}
} catch(AddressStringException e) {
System.out.println(addrString + " is invalid: " + e.getMessage());
}
}使用问题中提供的例子,上述方法的输出如下:
abcd:ef01:2345:6789:abcd:ef01:2345:6789 has no prefix length
2001:db8::8:800:200c:417a has no prefix length
ff01::101 has no prefix length
::1 has no prefix length
:: has no prefix length
2001:db8::8:800:200c:417a has no prefix length
ff01::101 has no prefix length
::1 has no prefix length
:: has no prefix length
::d01:4403 has no prefix length
::ffff:8190:3426 has no prefix length
::d01:4403 has no prefix length
FFFF:129.144.52.38 is invalid: FFFF:129.144.52.38 IP Address error: address has too few segments
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:0DB8:0:CD3/60 is invalid: 2001:0DB8:0:CD3/60 IP Address error: address has too few segments
2001:db8::cd30/60 has host address 2001:db8::cd30 and prefix length 60
2001:db8::cd3/60 has host address 2001:db8::cd3 and prefix length 60如你所见,关于FFFF:129.144.52.38是有效的,大约2001年:Db8::cd3 30/60和2001:Db8:Db8:CD3/60无效,这个问题是不正确的。第一个条件是::FFFF:129.144.52.38
https://stackoverflow.com/questions/5963199
复制相似问题