私が使用している文字列パターンの中には、実行時に一致文字を取得しないものがありますが、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,\\.]+):
'場合(m.find()){' - > 'HTTPを参照してください –
(m.find()){'、および使用する必要が入れ子になった 'm.find'ない間:// ideone。 com/PVauJK –