gpt4 book ai didi

java - 从 Ping 统计数据中提取数据的正则表达式没有输出。如何修复它?

转载 作者:行者123 更新时间:2023-11-30 07:14:53 25 4
gpt4 key购买 nike

我有以下字符串,这是我在 ping 结果末尾收到的字符串,我希望使用 java 中的正则表达式提取 min、avg、max 和 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]

关于java - 从 Ping 统计数据中提取数据的正则表达式没有输出。如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com