如何使用正则表达式解析ping结果

时间:2013-07-11 11:43:31

标签: java android regex ping

我想要的是逐行解析ping的结果。这对我来说有点棘手并尝试了很多东西但是......我在Android上使用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毫秒等

如果直接ping一个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 个答案:

答案 0 :(得分:4)

描述

该表达式将捕获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

答案 1 :(得分:0)

我认为你只需要解析第二行。

因此:

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

编辑为整个输入(第一个方案)添加了示例,为Pattern.MULTILINE添加了Pattern标记。