首页 > 代码库 > java 使用正则表达示判断是不是合法的IPV4IPV6地址
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地址
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。