从 Ping 统计信息中提取数据的正则表达式没有输出。如何解决?
No output from regex extracting data from Ping statistics. How to fix it?
我有以下字符串,这是我在 ping 结果末尾收到的字符串,我希望在 java
中使用正则表达式提取最小值、平均值、最大值和 mdev
[1463895327]PING www.gov.bw (168.167.134.24) 100(128) bytes of data.
[1463895327]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=1 ttl=110 time=868 ms
[1463895328]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=2 ttl=110 time=892 ms
[1463895329]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=3 ttl=110 time=814 ms
[1463895330]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=4 ttl=110 time=1009 ms
[1463895331]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=5 ttl=110 time=1006 ms
[1463895332]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=6 ttl=110 time=984 ms
[1463895333]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=7 ttl=110 time=1004 ms
[1463895334]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=8 ttl=110 time=1006 ms
[1463895335]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=9 ttl=110 time=1013 ms
[1463895336]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=10 ttl=110 time=578 ms
[1463895336]
[1463895336]--- www.gov.bw ping statistics ---
[1463895336]10 packets transmitted, 10 received, 0% packet loss, time 9007ms
[1463895336]rtt min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms, pipe 2
我想从子字符串中提取值
min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms
允许我将这 4 个值提取到 double 类型数组的正则表达式是什么?预期结果是
的数组
[274.175,430.739,818.328,147.779]
我已经尝试过表达式:
rtt\s+min\/avg\/max\/mdev\s+=\s+([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\s+ms
使用
// No error but all values null
public static String[] parsePingStatisticsMinAvgMaxMdev(String input) throws TimeNotFoundException {
// Capture the rtt min/avg/max/mdev times
Pattern p = Pattern.compile("rtt\s+min\/avg\/max\/mdev\s+=\s+([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\s+ms");
Matcher m = p.matcher(input);
if (m.find()){
int i = 0;
String[] s = new String[4];
while(m.find()){
s[i] = m.group(++i);
}
return s;
}
else
throw new TimeNotFoundException();
}
我没有输出(下图)。我该如何解决这个问题?
"C:\Program Files\Java\jdk1.8.0_71\bin\java" ...
Process finished with exit code 0
你快到了!
public class Main {
static String s = "[1463895327]PING www.gov.bw (168.167.134.24) 100(128) bytes of data.\n" +
"[1463895327]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=1 ttl=110 time=868 ms\n" +
"[1463895328]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=2 ttl=110 time=892 ms\n" +
"[1463895329]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=3 ttl=110 time=814 ms\n" +
"[1463895330]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=4 ttl=110 time=1009 ms\n" +
"[1463895331]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=5 ttl=110 time=1006 ms\n" +
"[1463895332]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=6 ttl=110 time=984 ms\n" +
"[1463895333]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=7 ttl=110 time=1004 ms\n" +
"[1463895334]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=8 ttl=110 time=1006 ms\n" +
"[1463895335]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=9 ttl=110 time=1013 ms\n" +
"[1463895336]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=10 ttl=110 time=578 ms\n" +
"[1463895336]\n" +
"[1463895336]--- www.gov.bw ping statistics ---\n" +
"[1463895336]10 packets transmitted, 10 received, 0% packet loss, time 9007ms\n" +
"[1463895336]rtt min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms, pipe 2";
public static void main(String args[]) throws IOException {
System.out.print(Arrays.toString(parsePingStatisticsMinAvgMaxMdev(s)));
}
public static String[] parsePingStatisticsMinAvgMaxMdev(String input) { throws TimeNotFoundException {
// Capture the rtt min/avg/max/mdev times
Pattern p = Pattern p = Pattern.compile("rtt\s+min\/avg\/max\/mdev\s+=\s+(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\s+ms");
Matcher m = p.matcher(input);
if (m.find()) {
int i = 1;
String[] s = new String[4];
while (m.find(i) && i <= 4) {
s[i - 1] = m.group(i);
i++;
}
return s;
} else
throw new TimeNotFoundException();
}
}
输出:
[578.263, 917.875, 1013.707, 132.095]
我有以下字符串,这是我在 ping 结果末尾收到的字符串,我希望在 java
中使用正则表达式提取最小值、平均值、最大值和 mdev[1463895327]PING www.gov.bw (168.167.134.24) 100(128) bytes of data.
[1463895327]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=1 ttl=110 time=868 ms
[1463895328]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=2 ttl=110 time=892 ms
[1463895329]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=3 ttl=110 time=814 ms
[1463895330]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=4 ttl=110 time=1009 ms
[1463895331]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=5 ttl=110 time=1006 ms
[1463895332]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=6 ttl=110 time=984 ms
[1463895333]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=7 ttl=110 time=1004 ms
[1463895334]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=8 ttl=110 time=1006 ms
[1463895335]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=9 ttl=110 time=1013 ms
[1463895336]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=10 ttl=110 time=578 ms
[1463895336]
[1463895336]--- www.gov.bw ping statistics ---
[1463895336]10 packets transmitted, 10 received, 0% packet loss, time 9007ms
[1463895336]rtt min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms, pipe 2
我想从子字符串中提取值
min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms
允许我将这 4 个值提取到 double 类型数组的正则表达式是什么?预期结果是
的数组[274.175,430.739,818.328,147.779]
我已经尝试过表达式:
rtt\s+min\/avg\/max\/mdev\s+=\s+([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\s+ms
使用
// No error but all values null
public static String[] parsePingStatisticsMinAvgMaxMdev(String input) throws TimeNotFoundException {
// Capture the rtt min/avg/max/mdev times
Pattern p = Pattern.compile("rtt\s+min\/avg\/max\/mdev\s+=\s+([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\s+ms");
Matcher m = p.matcher(input);
if (m.find()){
int i = 0;
String[] s = new String[4];
while(m.find()){
s[i] = m.group(++i);
}
return s;
}
else
throw new TimeNotFoundException();
}
我没有输出(下图)。我该如何解决这个问题?
"C:\Program Files\Java\jdk1.8.0_71\bin\java" ...
Process finished with exit code 0
你快到了!
public class Main {
static String s = "[1463895327]PING www.gov.bw (168.167.134.24) 100(128) bytes of data.\n" +
"[1463895327]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=1 ttl=110 time=868 ms\n" +
"[1463895328]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=2 ttl=110 time=892 ms\n" +
"[1463895329]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=3 ttl=110 time=814 ms\n" +
"[1463895330]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=4 ttl=110 time=1009 ms\n" +
"[1463895331]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=5 ttl=110 time=1006 ms\n" +
"[1463895332]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=6 ttl=110 time=984 ms\n" +
"[1463895333]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=7 ttl=110 time=1004 ms\n" +
"[1463895334]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=8 ttl=110 time=1006 ms\n" +
"[1463895335]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=9 ttl=110 time=1013 ms\n" +
"[1463895336]108 bytes from www.gov.bw (168.167.134.24): icmp_seq=10 ttl=110 time=578 ms\n" +
"[1463895336]\n" +
"[1463895336]--- www.gov.bw ping statistics ---\n" +
"[1463895336]10 packets transmitted, 10 received, 0% packet loss, time 9007ms\n" +
"[1463895336]rtt min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms, pipe 2";
public static void main(String args[]) throws IOException {
System.out.print(Arrays.toString(parsePingStatisticsMinAvgMaxMdev(s)));
}
public static String[] parsePingStatisticsMinAvgMaxMdev(String input) { throws TimeNotFoundException {
// Capture the rtt min/avg/max/mdev times
Pattern p = Pattern p = Pattern.compile("rtt\s+min\/avg\/max\/mdev\s+=\s+(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)\s+ms");
Matcher m = p.matcher(input);
if (m.find()) {
int i = 1;
String[] s = new String[4];
while (m.find(i) && i <= 4) {
s[i - 1] = m.group(i);
i++;
}
return s;
} else
throw new TimeNotFoundException();
}
}
输出:
[578.263, 917.875, 1013.707, 132.095]