IP合法判断包含IPv4和IPv6
IP合法判断包含IPv4和IPv6
/** 标准IPv4地址的正则表达式 */ public static final Pattern IPV4_REGEX = Pattern.compile( "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); /** 无全0块,标准IPv6地址的正则表达式 */ public static final Pattern IPV6_STD_REGEX = Pattern.compile( "^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); /** 压缩正则表达式 */ public static final Pattern IPV6_COMPRESS_REGEX = Pattern.compile( "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4})*)?)::((([0-9A-Fa-f]{1,4}:)*[0-9A-Fa-f]{1,4})?)$"); /** * Is ipv 4 boolean. */ public static boolean isIpv4(String ipv4) { return GlobalConst.IPV4_REGEX.matcher(ipv4).matches(); } /** * Is ipv 6 boolean. */ public static boolean isIpv6(String ipv6) { // 8*4 位数字,加上七个: ipv6地址长度不可能超过39位 if (ipv6.length() > GlobalConst.NUM_39) { return false; } return GlobalConst.IPV6_STD_REGEX.matcher(ipv6).matches() || GlobalConst.IPV6_COMPRESS_REGEX.matcher(ipv6).matches(); }