2016-07-21 44 views
0

私が使用している文字列パターンの中には、実行時に一致文字を取得しないものがありますが、Regular Expression Testerなどの正規表現チェッカーをオンラインで使用すると、正規表現パターンの一致が一致しません

public class RegexMatches 
{ 
    public static void main(String args[]){ 
     String input = "[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms"; 

     try{ 
      String op[] = parseIndividualIP(input); 
      System.out.println(op[1]);    
      System.out.println(op[8]);     
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

// Doesn't Work... 
    public static String[] parseIndividualIP(String input) throws IPAddressNotFoundException { 
     // Capture the ip-address after 'x byes from' 
     Pattern p = Pattern.compile("bytes\\s+from\\s+([\\d,\\.]+):"); 
     Matcher m = p.matcher(input); 
     if (m.find()){ 
      int i = 0; 
      String[] s = new String[10]; 
      while(m.find()){ 
       s[i] = m.group(++i); 
      } 
      return s; 
     } 
     else 
      throw new IPAddressNotFoundException(); 
    } 

} 

なぜ実行時に一致がなく、この問題をどのようにデバッグする必要があるのか​​分かりません。パターンは実行前と実行後にクロスチェックされました。

入力文字列 - 使用さ

[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms 

正規表現パターン -

bytes\\s+from\\s+([\\d,\\.]+): 
+0

'場合(m.find()){' - > 'HTTPを参照してください –

+0

(m.find()){'、および使用する必要が入れ子になった 'm.find'ない間:// ideone。 com/PVauJK –

答えて

1

主な問題はm.group(++i)を使用しています。正規表現では、IPアドレス(([\\d,\\.]+))をキャプチャするキャプチャグループが1つあります。つまり、m.group(1)を呼び出すと、最初のグループでキャプチャされた文字列が返されます。

Patternオブジェクトはスレッドセーフなので、一度コンパイルすると同じインスタンスを再利用できます。

次のコードには、グループ修正と、読みやすくするための変更が含まれています。配列の使用法をリンクリストに変更しました。例外をスローする代わりに、メソッドが返ります。一致するものが見つからない場合は空のリストが返されます。

import java.util.LinkedList; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class RegexMatches { 
    private static final Pattern PATTERN = Pattern.compile("bytes\\s+from\\s+([\\d,\\.]+):"); 

    public static void main(String args[]) { 
     String input = "[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms"; 

     List<String> op = parseIndividualIP(input); 
     op.forEach(System.out::println); 
    } 

    public static List<String> parseIndividualIP(String input) { 
     Matcher m = PATTERN.matcher(input); 
     List<String> ips = new LinkedList<>(); 
     while (m.find()) { 
      ips.add(m.group(1)); 
     } 
     return ips; 
    } 
} 
+0

魅力のように動作します。ありがとう! –

関連する問題