1、过滤掉错的数据

    @Test
    public void  test2()
    {
        String ipText ="172.16.1.198\r\n172.16.3.50\r\n1245.3.2154.5";
        Iterable<String> it = Splitter.on("\r\n").trimResults().omitEmptyStrings().split(ipText);
        UnmodifiableIterator<String> filtered = Iterators.filter(it.iterator(), ip -> NetUtils.isIllLegalIp(ip));
        while (filtered.hasNext())
        {
            String ip = filtered.next();
            System.out.println(ip);
        }
    }

 

import java.util.regex.Pattern;

public final class NetUtils {

    private NetUtils() {
    }

    private static final Pattern ipPattern = Pattern.compile("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9]).(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d).(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d).(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");

    public static boolean isIllLegalIp(final String ip)
    {
        return ipPattern.matcher(ip).matches();
    }
}

 

执行结果

 

posted on 2020-01-07 14:29  你不知道的浪漫  阅读(346)  评论(0编辑  收藏  举报