2013-07-29 106 views
1

我如何解析這個字符串:解析純文本

PING 192.168.1.2(192.168.1.2)56(84)從192.168.1.2 data.64字節的字節數:icmp_seq = 1個TTL = 64個時間= 0.244 ms64字節來自192.168.1.2:icmp_seq = 2 ttl = 64時間= 0.274 ms64字節來自192.168.1.2:icmp_seq = 3 ttl = 64時間= 0.275 ms64字節來自192.168.1.2:icmp_seq = 4 ttl = 64時間= 0.306來自192.168.1.2的ms64字節:icmp_seq = 5 ttl = 64時間= 0.550 ms --- 192.168.1.2 ping statistics ---發送了5個數據包,收到了5個數據包,發生了0%的數據包丟失,時間4001msrtt min/avg/max/mdev = 0.244/0.329/0.550/0.114 ms

我試過用StringTokenizer但沒有得到適當的結果。

代碼我想是遵循:

StringTokenizer tokens = new StringTokenizer(pingResult, ":"); 
String first = tokens.nextToken();// this will contain "Fruit" 
String second = tokens.nextToken(); 
+1

什麼應該是解析的字符串結果? –

+1

預期產量是多少? – ranjit

+1

@BrijeshThakur預期結果應該是「icmp_seq = 1 ttl = 64時間= 0.244毫秒」 – URAndroid

回答

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); 

    } 
+2

我想上面的解決方案會給你「icmp_seq = 1 ttl = 64時間= 0.244 ms64字節從192.168.1.2」這不是預期的。 –

+0

好的,我已經更新了我的答案! –

2

試試這個,

while(tokens.hasMoreElements()) 
{ 
    String subStringValue = tokens.nextToken(); 
    System.out.println("token : " + StringUtils.substringBetween(subStringValue, "", "ms")+"ms"); 
} 

StringUtils.substringBetween

2

當家StringTokenizer將無法​​正常工作。 StringTokenizersubstring可用於實現預期的結果。即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); 

雖然答案有點骯髒,但它解決了目的。

3

嘗試一個正則表達式的匹配:

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毫秒

icmp_seq = 2 TTL = 64時間= 0.274毫秒

icmp_seq = 3 ttl = 64時間= 0.275 ms

icmp_seq = 4 ttl = 64時間= 0.306 ms

icmp_seq = 5 ttl = 64 time = 0.550 ms