您好,我有一个包含跟踪路由和 ping 的日志文件。 我用
将它们分开if (scanner.nextLine ().startsWith ("64 bytes"){}
所以我现在可以只使用 ping。
我对 ping 感兴趣的是 time=XX
示例数据行 =
64 bytes from ziva.zarnet.ac.zw (209.88.89.132): icmp_seq=119 ttl=46 time=199 ms
我一直在阅读其他人的类似问题,但我不知道如何应用到我的问题。
我实际上只需要数字,因为我将把它们放入 csv 文件中,这样我就可以制作数据图表。
编辑:使用 Robins 解决方案,我现在可以在屏幕上显示我的 ping,但它会执行其他所有操作并错过第一个。
while (scanner.hasNextLine ()) {
//take only pings.
if (scanner.nextLine ().startsWith ("64 bytes")){
String line = scanner.nextLine ();
String pingAsString = line.substring (line.lastIndexOf ("=") + 1, (line.length () - "ms".length ()));
Double ping = Double.valueOf (pingAsString);
System.out.println ("PING AS STRING = "+ping);
}
}
已排序。那只是需要移动线路分配。大写字母。但说清楚了。 :D
最佳答案
尝试使用 RegularExpression提取您需要的数据:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExTest {
public static void main(String[] args) {
String test = "line= 14103 64 bytes from ziva.zarnet.ac.zw (209.88.89.132): icmp_seq=119 ttl=46 time=199 ms";
// build the regular expression string
String regex = ".*time=(\\d+).*";
// compile the regular expresion into a Pattern we can use on the test string
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(test);
// if the regular expression matches, grab the value matching the
// expression in the first set of parentheses: "(\d+)"
if (matcher.matches()) {
System.out.println(matcher.group(1));
}
}
}
关于java - 获取线路的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9853511/