匹配IP地址的正则表达式实例

前端之家收集整理的这篇文章主要介绍了匹配IP地址的正则表达式实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.accel.dashboard.tool;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jason.zhan on 9/10/2014.
 */
public class IPScanner {

    private IPScanner() {}

    private static class IPScannerFactory {
        private static IPScanner instance = new IPScanner();
    }

    public static IPScanner getIPScanner() {
        return IPScannerFactory.instance;
    }

    public Object readResolve() {
        return getIPScanner();
    }

    /**
     * @return All the Server IP.
     *
     * @param cmd Your command.
     *
     */
    public Set<String> getServerIP(String cmd) {
        BufferedReader bufferedReader = null;
        Set<String> iPSet = new HashSet<String>();
        String command = "cmd.exe /c arp -a";
        if (cmd != null) {
            command = cmd;
        }
        try {
            Process process = Runtime.getRuntime().exec(command);
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String regex = "(?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
            String line = null;
            StringBuilder stringBuilder = new StringBuilder();
            Matcher matcher = null;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            matcher = Pattern.compile(regex).matcher(stringBuilder);
            while (matcher.find()) {
                iPSet.add(matcher.group(0));
            }
            //System.out.println(stringBuilder);
        } catch (Exception e1) {
            e1.printStackTrace();
            iPSet = null;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        return iPSet;
    }
}
原文链接:https://www.f2er.com/regex/361298.html

猜你在找的正则表达式相关文章