java进行ip号码段正则匹配

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,正则)