首页 > 编程语言 > 详细

java进行ip号码段正则匹配

时间:2015-01-21 11:31:56      阅读:627      评论:0      收藏:0      [点我收藏+]

java正则匹配IP号码段

public static boolean IPMatch(List ips, String ip) {
        if (ips.contains(ip)) {
            return true;
        }

        for (int i = 0; i < ips.size(); i++) {
            List lip = Arrays.asList(ips.get(i).toString().split("\\."));
            String re = "^";
            for (int j = 0; j < lip.size(); j++) {
                String num = lip.get(j).toString();
                if (num != "*") {
                    re += num + ".";
                } else {
                    re += "\\d{0,3}.";
                }
                if (j == lip.size()) {
                    re = re.substring(0, re.length() - 1).toString() + "\\$";
                }
            }

            Pattern pattern = Pattern.compile(re);
            Matcher matcher = pattern.matcher(ip);
            if (matcher.matches()) {
                return true;
            }
        }
        return false;
    }


java进行ip号码段正则匹配

原文:http://blog.csdn.net/zimu002/article/details/42965775

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!