标签:
直接看代码:
import java.util.regex.Pattern; public class App { private static Pattern pattern; private static Pattern ptipv4; static { // ipv6 pattern = Pattern.compile("^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}){1,2})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){1,3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){1,4})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){1,5})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){1,6})|(:(:[0-9A-Fa-f]{1,4}){1,7})|(([0-9A-Fa-f]{1,4}:){6}(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$"); // ipv4 ptipv4 = Pattern.compile("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$"); } public static void main( String[] args ) { System.out.println(pattern.matcher("2001:0DB8:0000:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:8:800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:8:800:192.1.0.0").matches()); System.out.println(pattern.matcher("2001:DB8:0::800:192.1.0.0").matches()); System.out.println(pattern.matcher("2001:DB8:0:23::192.1.0.0").matches()); System.out.println(pattern.matcher("::192.1.0.0").matches()); System.out.println(pattern.matcher("1:af::3").matches()); System.out.println(pattern.matcher("1:af::").matches()); System.out.println(pattern.matcher("::1:af:0").matches()); System.out.println(pattern.matcher("::0").matches()); System.out.println("---------------------"); System.out.println(pattern.matcher("+2001:0DB8:0000:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:0DB8:0z00:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:800::192.1.0.0.1").matches()); System.out.println(pattern.matcher("2001:DB8::23::800:192.1.0.0").matches()); System.out.println(pattern.matcher(":::").matches()); System.out.println(pattern.matcher("1:::2").matches()); System.out.println("---------------------"); System.out.println(ptipv4.matcher("1.1.0.1").matches()); System.out.println(ptipv4.matcher("123.1.0.19").matches()); System.out.println(ptipv4.matcher("255.255.255.255").matches()); System.out.println(ptipv4.matcher("0.0.0.0").matches()); System.out.println("---------------------"); System.out.println(ptipv4.matcher("-1.1.0.1").matches()); System.out.println(ptipv4.matcher("1.1b.0.1").matches()); System.out.println(ptipv4.matcher("1.01.0.1").matches()); System.out.println(ptipv4.matcher("1.1.300.1").matches()); System.out.println(ptipv4.matcher("1.1..1").matches()); } }
java 使用正则表达示判断是不是合法的IPV4IPV6地址
标签:
原文地址:http://www.cnblogs.com/re1n/p/5834526.html