有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何用regex解析ping结果

我想要的是逐行解析ping的结果。这对我来说有点棘手,我尝试了很多东西,但是。。。我在安卓上使用ping

例如:

PING google.com (173.194.35.9) 56(84) bytes of data.
64 bytes from mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms

在第一行,我想要Ip地址,“56(84)字节的数据”。第二行“64字节”,1,52,33.0毫秒等

如果直接使用平安IP,它会有一点变化

PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms

--- 192.168.0.12 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms

但也应该有效

如果我有一个小解释和一个答案,它会很酷

非常感谢


共 (2) 个答案

  1. # 1 楼答案

    描述

    这个表达式将捕获IP、数据字节、字节、ICMP_SEQ、ttl、时间。我找不到etc

    ^PING\b # match ping
    [^(]*\(([^)]*)\) # capture IP
    \s([^.]*)\. # capture the bytes of data
    .*?^(\d+\sbytes)  # capture bytes
    .*?icmp_seq=(\d+)  # capture icmp_seq
    .*?ttl=(\d+)  # capture ttl
    .*?time=(.*?ms)  # capture time
    .*?(\d+)\spackets\stransmitted   # the rest of these lines will capture the other portions of the ping result
    .*?(\d+)\sreceived
    .*?(\d+%)\spacket\sloss
    .*?time\s(\d+ms)
    .*?=\s([^\/]*)\/([^\/]*)\/([^\/]*)\/(.*)\sms
    

    enter image description here

    范例

    实例:http://www.rubular.com/r/uEDoEZwY7U

    示例文本

    PING google.com (173.194.35.9) 56(84) bytes of data.
    64 bytes from mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms
    
     - google.com ping statistics  -
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms
    
    
    PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
    64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms
    
     - 192.168.0.12 ping statistics  -
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms
    

    示例代码

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    class Module1{
      public static void main(String[] asd){
      String sourcestring = "source string to match with pattern";
      Pattern re = Pattern.compile("^PING\\b # match ping
    [^(]*\\(([^)]*)\\) # capture IP
    \\s([^.]*)\\. # capture the bytes of data
    .*?^(\\d+\\sbytes) # capture bytes
    .*?icmp_seq=(\\d+) # capture icmp_seq
    .*?ttl=(\\d+) # capture ttl
    .*?time=(.*?ms) # capture time
    .*?(\\d+)\\spackets\\stransmitted
    .*?(\\d+)\\sreceived
    .*?(\\d+%)\\spacket\\sloss
    .*?time\\s(\\d+ms)
    .*?=\\s([^\\/]*)\\/([^\\/]*)\\/([^\\/]*)\\/(.*?)\\sms
    
    
    ",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      Matcher m = re.matcher(sourcestring);
      int mIdx = 0;
        while (m.find()){
          for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
            System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
          }
          mIdx++;
        }
      }
    }
    

    捕获组

    [0][0] = PING google.com (173.194.35.9) 56(84) bytes of data.
    64 bytes from mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms
    
     - google.com ping statistics  -
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms
    [0][2] = 173.194.35.9
    [0][2] = 56(84) bytes of data
    [0][3] = 64 bytes
    [0][4] = 1
    [0][5] = 52
    [0][6] = 33.0 ms
    [0][7] = 1
    [0][8] = 1
    [0][9] = 0%
    [0][10] = 0ms
    [0][11] = 33.086
    [0][12] = 33.086
    [0][13] = 33.086
    [0][14] = 0.000
    
    
    [1][0] = PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
    64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms
    
     - 192.168.0.12 ping statistics  -
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms
    [1][3] = 192.168.0.12
    [1][2] = 56(84) bytes of data
    [1][3] = 64 bytes
    [1][4] = 1
    [1][5] = 64
    [1][6] = 0.134 ms
    [1][7] = 1
    [1][8] = 1
    [1][9] = 0%
    [1][10] = 0ms
    [1][11] = 0.134
    [1][12] = 0.134
    [1][13] = 0.134
    [1][14] = 0.000
    
  2. # 2 楼答案

    我想你只需要解析第二行

    因此:

    String domainPing = "64 bytes from mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms";
    String ipPing = "64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms";
    String wholeDomainPing = "PING google.com (173.194.35.9) 56(84) bytes of data.\r\n"+
                "64 bytes from mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms\r\n\r\n"+
                " - google.com ping statistics  -\r\n"+
                "1 packets transmitted, 1 received, 0% packet loss, time 0ms\r\n" +
                "rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms";
    Pattern pattern = Pattern.compile(
    // "[digit] bytes"..... "from [ip]"               or "([ip])"
    "(\\d+(?=\\sbytes)).*?(((?<=(from\\s))[\\d\\.]+)|((?<=\\()[\\d\\.]+(?=\\))))",
    Pattern.MULTILINE
    );
    Matcher matcher = pattern.matcher(domainPing);
    if (matcher.find()) {
        System.out.println("Bytes: " + matcher.group(1));
        System.out.println("IP: " + matcher.group(2));
    }
    matcher = pattern.matcher(ipPing);
    if (matcher.find()) {
        System.out.println("Bytes: " + matcher.group(1));
        System.out.println("IP: " + matcher.group(2));
    }
    matcher = pattern.matcher(wholeDomainPing);
    if (matcher.find()) {
        System.out.println("Bytes: " + matcher.group(1));
        System.out.println("IP: " + matcher.group(2));
    }
    // etc...
    

    输出:

    Bytes: 64
    IP: 173.194.35.9
    Bytes: 64
    IP: 192.168.0.12
    Bytes: 64
    IP: 173.194.35.9
    

    Edit为整个输入(第一个场景)添加了示例,并为Pattern添加了Pattern.MULTILINE标志