寻找一个字符串,可以通过String#matches(String)方法匹配IPv4,另外一个字符串可以匹配IPv6。
public static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_HEX4DECCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::((?:[0-9A-Fa-f]{1,4}:)*)(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_6HEX4DEC_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_HEXCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)\\z";
public static final String IPV6_REGEX = "\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z";
从某个博客上得到这些信息。对于正则表达式熟练的人应该能够为所有IPv6地址类型设计出单一的正则表达式。实际上,我想你可以有一个单一的正则表达式来匹配IPv4和IPv6。
以下是匹配IPv4地址的正则表达式:
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
当您在Java中将其指定为字符串文字时,需要转义反斜杠:
"\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"
package com.capgemini.basics;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Main {
private static Pattern VALID_IPV4_PATTERN = null;
private static Pattern VALID_IPV6_PATTERN1 = null;
private static Pattern VALID_IPV6_PATTERN2 = null;
private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
private static final String ipv6Pattern1 = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";
private static final String ipv6Pattern2 = "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$";
static {
try {
VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);
VALID_IPV6_PATTERN1 = Pattern.compile(ipv6Pattern1, Pattern.CASE_INSENSITIVE);
VALID_IPV6_PATTERN2 = Pattern.compile(ipv6Pattern2, Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
System.out.println("Neither");
}
}
public static List<String> validateAddresses(List<String> ipAddress) {
final List<String> validity= new ArrayList<String>();
int len = ipAddress.size();
for(int i=0; i<len; i++){
Matcher m1 = Main.VALID_IPV4_PATTERN.matcher(ipAddress.get(i));
Matcher m12 = Main.VALID_IPV6_PATTERN1.matcher(ipAddress.get(i));
Matcher m22 = Main.VALID_IPV6_PATTERN2.matcher(ipAddress.get(i));
if (m1.matches()) {
validity.add("IPv4");
}
else if(m12.matches() || m22.matches()){
validity.add("IPv6");
}
else{
validity.add("Neither");
}
}
return validity;
}
public static void main(String[] args)
{
final List<String> IPAddress = new ArrayList<String>();
//Test Case 0
/*IPAddress.add("121.18.19.20");
IPAddress.add("0.12.12.34");
IPAddress.add("121.234.12.12");
IPAddress.add("23.45.12.56");
IPAddress.add("0.1.2.3");*/
//Test Case 1
/*IPAddress.add("2001:0db8:0000:0000:0000:ff00:0042:8329");
IPAddress.add("2001:0db8:0:0:0:ff00:42:8329");
IPAddress.add("::1");
IPAddress.add("2001:0db8::ff00:42:8329");
IPAddress.add("0000:0000:0000:0000:0000:0000:0000:0001");*/
//Test Case 2
/*IPAddress.add("000.012.234.23");
IPAddress.add("666.666.23.23");
IPAddress.add(".213.123.23.32");
IPAddress.add("23.45.22.32.");
IPAddress.add("272:2624:235e:3bc2:c46d:682:5d46:638g");
IPAddress.add("1:22:333:4444");*/
final List<String> result = validateAddresses(IPAddress);
for (int i=0; i<result.size(); i++)
System.out.println(result.get(i)+" ");
}
}
正则表达式允许在IPv4部分使用前导0。
一些Unix和Mac发行版将这些段转换为八进制。
我建议使用25 [0-5] | 2 [0-4] \d | 1 \d \ d | [1-9]? \ D
作为IPv4段。
当考虑到包含嵌入式IPv4和压缩地址的地址时,ipv6的正则表达式可能会变得非常棘手。
开源IPAddress Java库将验证IPv6和IPv4的所有标准表示形式,并支持前缀长度(以及对其进行验证)。免责声明:我是该库的项目经理。
代码示例:
try {
IPAddressString str = new IPAddressString("::1");
IPAddress addr = str.toAddress();
} catch(AddressStringException e) {
//e.getMessage has validation error
}