How Can I parse this string:
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
I tried using StringTokenizer
but not getting appropriate result.
Code I tried is follow:
StringTokenizer tokens = new StringTokenizer(pingResult, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();
Try a regex match:
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()));
}
}
Result:
icmp_seq=1 ttl=64 time=0.244 ms
icmp_seq=2 ttl=64 time=0.274 ms
icmp_seq=3 ttl=64 time=0.275 ms
icmp_seq=4 ttl=64 time=0.306 ms
icmp_seq=5 ttl=64 time=0.550 ms
Try this,
while(tokens.hasMoreElements())
{
String subStringValue = tokens.nextToken();
System.out.println("token : " + StringUtils.substringBetween(subStringValue, "", "ms")+"ms");
}
Alone StringTokenizer
won't work. StringTokenizer
with substring
can be used to achieve the expected result. ie StringTokenizer
will give tokens as " icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2
". You can do substring from the tokenized String. something like :
String finalString=tokenizedString.substring(0,tokenizedString.indexOf(ms)+1);
Though the answer is a bit dirty but it solves the purpose.
You can use String split method.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.