Java 正则表达式匹配器显示意外结果

标签 java regex parsing pattern-matching ping

我试图使用正则表达式从标准 ping 命令收集的输出中解析数据。然而,即使在在线正则表达式检查器中检查了正则表达式之后,某些模式也无法按预期工作(它们在浏览器中工作正常)。

我收到的错误如下:

Exception in thread "main" java.lang.IllegalStateException: No match found
    at java.util.regex.Matcher.group(Matcher.java:536)
    at RegexMatches.parseGroupBytes(RegexMatches.java:77)
    at RegexMatches.main(RegexMatches.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

鉴于我缺乏使用正则表达式的经验,我想知道如何继续解决问题。我使用的类如下:

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";
        String input2 = "[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\n";
//        System.out.println(parse1(input));
        try{
            String op[] = parseGroupBytes(input);
            System.out.println(op[0]);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String parse1(String input){
        Pattern p =Pattern.compile("\\[([0-9]{10})\\]PING");
        Matcher m = p.matcher(input);
        if (m.find())
            return m.group(1);
        else
            return "error";
    }

// Doesn't Work...
    // patterns seem to be correct but only shows the first value

    public static String[] parseGroupBytes(String input) throws BytesNotFoundException {
        //  Capture the bytes after (ip-address) outside the parenthesis
        //  Capture the bytes after (ip-address) inside the parenthesis
        Pattern p1 = Pattern.compile("\\)\\s+(\\d+)\\(");
        Matcher m1 = p1.matcher(input);
        Pattern p2 = Pattern.compile("\\((\\d+)\\)\\s+bytes");
        Matcher m2 = p2.matcher(input);
        String[] GroupBytes = new String[2];
        int x = m1.groupCount();
        int y = m2.groupCount();
        if(m1.find() || m2.find()){
            GroupBytes[0] = m1.group(1);
            GroupBytes[1] = m2.group(1);
              return GroupBytes;
        }
        else
            throw new BytesNotFoundException();
    }
}

最佳答案

问题出在这个 block 上:

if(m1.find() || m2.find()){
    GroupBytes[0] = m1.group(1);
    GroupBytes[1] = m2.group(1);
    return GroupBytes;
}

由于您进入 if 条件,如果匹配器 m1m2 的任何匹配成功,但在执行 m2 时.group(1) 它将抛出 IllegalStateException 因为 m2.find() 由于 || 永远不会被执行 m1.find() 返回 true。

更改该 block 以使用 && 而不是 ||:

if(m1.find() && m2.find()){
    GroupBytes[0] = m1.group(1);
    GroupBytes[1] = m2.group(1);
    return GroupBytes;
}
else
    throw new BytesNotFoundException();

现在,在调用 .group(1) 之前,代码将为匹配器 m1m2 执行 find() > 对于每个匹配器对象。

关于Java 正则表达式匹配器显示意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489069/

相关文章:

java - 如何比较多个流的结果并返回最高结果

java - springmvc osx windows servlet maven

java - 在 Spring redis session 中保存对象时出现 ClassCastException

python - 如何使用正则表达式在Python中删除模式之前和之后的空格?

python - 自然排序

javascript - 将 Javascript 代码转换/转义为字符串以放置在 HTML 页面上

java - 模拟抽象类并使用 Mockito 注释注入(inject)类?

Python 使用 REGEX 删除文本中的标点符号

string - 如何在Haskell中解析IO字符串?

json - 预期对象或值 : Importing JSON into Python (Pandas)