码迷,mamicode.com
首页 > 编程语言 > 详细

java进行ip号码段正则匹配

时间:2015-01-21 11:33:27      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:java 正则

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号码段正则匹配

标签:java 正则

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!