2016-07-26 17 views
1

Ich habe die folgende Zeichenfolge das ist, was ich am Ende eines Ping-Ergebnis erhalten haben, möchte ich die min, avg, max und mdev mit Regex in Java extrahierenKeine Ausgabe von Regex, die Daten aus Ping-Statistiken extrahiert. Wie man es repariert?

[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 

ich die Werte extrahieren möchten von der Teilkette

min/avg/max/mdev = 578.263/917.875/1013.707/132.095 ms 

Was würde die regulären Ausdruck sein, die mir diese vier Werte ein Array vom Typ double zu extrahieren erlauben würde? Das erwartete Ergebnis ist ein Array mit

[274.175,430.739,818.328,147.779] 

habe ich bereits versucht, den Ausdruck:

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 

Mit

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

} 

ich keine Ausgabe zu erhalten (siehe unten). Wie behebe ich das?

"C:\Program Files\Java\jdk1.8.0_71\bin\java" ... 

Process finished with exit code 0 
+0

Können Sie nach einem [MCVE]? –

+0

Sollte das erwartete Ergebnis nicht ein Array mit '578.263/917.875/1013.707/132.095' sein?. Ich sehe' 274.175..' nirgendwo. – TheLostMind

+0

Auf einer Randnotiz: Sie können auch '\\ d +' tun, um Ziffern in regexp darzustellen. Beispiel: 'Muster p = Pattern.compile (" min \\/avg \\/max \\/mdev \\ s + = \\ s + (\\ d + \\. \\ d +) \\/(\\ d + \ \. \\ d +) \\/(\\ d + \\. \\ d +) \\/(\\ d + \\. \\ d +) \\ s + ms ");' –

Antwort

1

Sie waren fast da!

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

Ausgang:

[578,263, 917,875, 1013,707, 132,095]

+0

Danke! Funktioniert wie ein Traum –

+0

Bitte schließen Sie diese Frage mit markierender Antwort als richtig. – Divers