正则表达式模式未获取匹配项

Regex pattern fetching no matches

我使用的一些字符串模式在运行时没有为我获取匹配项,而当我使用 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.group(++i)。在您的正则表达式中,您有一个捕获 IP 地址的捕获组 (([\d,\.]+)),这意味着您应该调用 m.group(1) 作为第一组捕获的字符串 returns。

Pattern 对象是线程安全的,因此它可以被编译一次并重复使用同一个实例。

以下代码包含组修复以及一些为了提高可读性而进行的修改。将数组的用法更改为链接列表,而不是抛出异常方法 returns 和空列表,如果没有找到匹配项。

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