java正则解析ip

2021-04-22 06:29

阅读:555

标签:ISE   auto   lis   append   ast   nal   star   pat   start   

public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
String ipsearch="11.168.1.101-103/126/129/200-204";
ipAnalys(ipsearch);

}

private static List ipAnalys(String ipsearch) {
String[] ips=ipsearch.split("/");
String pattern = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)";

List realIps=new ArrayList();
String ipFrist="";
for (int i = 0; i StringBuffer realip=new StringBuffer();
String ip=ips[i];
if(Pattern.matches(pattern, ip)){
realIps.add(ips[i]);
ipFrist=ips[i].substring(0,ip.lastIndexOf("."));
}else{
if(ips[i].indexOf("-") realip.append(ipFrist).append(".").append(ips[i]);
realIps.add(realip.toString());
}else{
String[] iprange=ips[i].split("-");
int start,end;
if(StringUtils.isEmpty(ipFrist)){
//说明直接是ip范围
ipFrist=ips[i].substring(0,ip.lastIndexOf("."));
start=Integer.parseInt(iprange[0].substring(ipFrist.length()+1));
end =Integer.parseInt(iprange[1]);
}else{
start=Integer.parseInt(iprange[0]);
end =Integer.parseInt(iprange[1]);

}
for (int j = start; j realip.setLength(0);
realip.append(ipFrist).append(".").append(j);
realIps.add(realip.toString());
}
}
}
}
return realIps;
}

}

java正则解析ip

标签:ISE   auto   lis   append   ast   nal   star   pat   start   

原文地址:https://www.cnblogs.com/javahepeng/p/13278519.html


评论


亲,登录后才可以留言!