java 正则验证IP地址是不是合法的怎么写?

不包含0段 0.0.0.0~0.255.255.255
2025-01-05 01:51:58
推荐回答(1个)
回答1:

String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";

Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher("192.168.0.150");
matcher.matches();//验证结果返回ture或者false