真正用到的时候,查找就发现很多都是错误的,所以还是自己整理将正确的代码记录一下吧。。
以下亲测可用,判断字符串是否符合IP地址格式
public static boolean isboolIp(String ipAddress) {
if (ipAddress.length() < 7 || ipAddress.length() > 15) {
return false;
}
/**
* 判断IP格式和范围
*/
String rexp = "([1-9]|[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}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(ipAddress);
boolean flag = mat.matches();
return flag;
}