java正则解析ip

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<String> 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<String> realIps=new ArrayList<String>();
String ipFrist="";
for (int i = 0; i < ips.length; 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("-")<0){
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 <= end; j++) {
realip.setLength(0);
realip.append(ipFrist).append(".").append(j);
realIps.add(realip.toString());
}
}
}
}
return realIps;
}

}

posted @ 2020-07-10 11:54  javahepeng  阅读(339)  评论(0编辑  收藏  举报