[英]Parse Plain Text
我如何解析该字符串:
PING 192.168.1.2(192.168.1.2)56(84)字节数据。从192.168.1.2起的64字节:icmp_seq = 1 ttl = 64时间= 0.244 ms64从192.168.1.2起的icmp字节:icmp_seq = 2 ttl = 64时间= 0.274 ms64来自192.168.1.2的字节:icmp_seq = 3 ttl = 64时间= 0.275 ms源自192.168.1.2的字节:icmp_seq = 4 ttl = 64时间= 0.306 ms来自192.168.1.2的64字节:icmp_seq = 5 ttl = 64时间= 0.550 ms-- -192.168.1.2 ping统计信息--- 5个数据包传输,5个接收,0%数据包丢失,时间4001msrtt最小值/平均值/最大值/ mdev = 0.244 / 0.329 / 0.550 / 0.114毫秒
我尝试使用StringTokenizer
,但未获得适当的结果。
我尝试的代码如下:
StringTokenizer tokens = new StringTokenizer(pingResult, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();
尝试进行正则表达式匹配:
public static void findMatch(){
String str = "PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.64 bytes from 192.168.1.2" +
": icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.274 ms64 " +
"bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.275 ms64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.306 ms64 " +
"bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.550 ms--- 192.168.1.2 ping statistics ---5 packets transmitted, " +
"5 received, 0% packet loss, time 4001msrtt min/avg/max/mdev = 0.244/0.329/0.550/0.114 ms";
String regexPattern = "icmp_seq=\\d+ ttl=\\d+ time=.+?ms";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
System.out.println(str.substring(matcher.start(), matcher.end()));
}
}
结果:
icmp_seq = 1 ttl = 64时间= 0.244 ms
icmp_seq = 2 ttl = 64时间= 0.274 ms
icmp_seq = 3 ttl = 64时间= 0.275 ms
icmp_seq = 4 ttl = 64时间= 0.306 ms
icmp_seq = 5 ttl = 64时间= 0.550 ms
尝试这个,
while(tokens.hasMoreElements())
{
String subStringValue = tokens.nextToken();
System.out.println("token : " + StringUtils.substringBetween(subStringValue, "", "ms")+"ms");
}
单独的StringTokenizer
无效。 带有substring
StringTokenizer
可用于实现预期结果。 即StringTokenizer
将给予令牌为“ icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2
”。 您可以从标记化字符串中执行子字符串。 就像是 :
String finalString=tokenizedString.substring(0,tokenizedString.indexOf(ms)+1);
答案虽然有点脏,但是却解决了目的。
您可以使用字符串拆分方法。
ArrayList<String> ResultTab= new ArrayList<String>();
String pingResult = "PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.274 ms64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.275 ms64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.306 ms64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.550 ms--- 192.168.1.2 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4001msrtt min/avg/max/mdev = 0.244/0.329/0.550/0.114 ms";
String[] pingResultTab = pingResult.split(":");
for (int i = 0; i < pingResultTab.length; i++) {
System.out.println(pingResultTab[i]);
if(i >= 1)
{
String[] itTab = pingResultTab[i].split("ms");
ResultTab.add(itTab[0]);
}
}
System.out.println("---RESULTAT-------");
for (String result : ResultTab) {
System.out.println(result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.