Java正则表达式提取String字符串中的IP地址

/**
     * 正则提前字符串中的IP地址
     * @param ipString
     * @return
     */
    public static List getIps(String ipString){        
        String regEx="((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; 
        List ips = new ArrayList();
        Pattern p = Pattern.compile(regEx); 
        Matcher m = p.matcher(ipString);
        while (m.find()) {
            String result = m.group();
            ips.add(result);
        }
        return ips;
    }

    
    public static void main(String[] args) {
        String ipString="!254.254.254.254 127.0.0.1localhost192.168.2.1localhost";
        System.out.println(getIps(ipString));
    }

 

转载于:https://www.cnblogs.com/xiehongwei/p/8931151.html

你可能感兴趣的:(Java正则表达式提取String字符串中的IP地址)