java 使用正则表达示判断是不是合法的IPV4IPV6地址
Posted 哥斯达黎加
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用正则表达示判断是不是合法的IPV4IPV6地址相关的知识,希望对你有一定的参考价值。
直接看代码:
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地址的主要内容,如果未能解决你的问题,请参考以下文章
java中正则表达式如何使用?比如判断一个字符串是不是满足某种格式,给个详细点的例子
急求一个JAVA中判断用户输入的数字是一个合法的电话号码的正则表达式,包括住宅电话与移动电话。