Java Regex匹配器显示意外结果

时间:2016-07-20 19:17:55

标签: java regex parsing pattern-matching ping

我试图使用Regex从标准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

鉴于我缺乏Regex的经验,我想知道如何解决这个问题。我使用的课程如下:

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

3 个答案:

答案 0 :(得分:4)

问题是这个块:

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

由于您在匹配ifm1的任何匹配成功后输入m2条件,但在执行m2.group(1)时,它会抛出IllegalStateException由于m2.find()||返回true,因此m1.find()永远不会被执行。

更改该块以使用&&代替||

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

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

答案 1 :(得分:0)

您正在m1.find() || m2.find(),所以如果m1找到匹配,则不会尝试m2,因此您应该更改它,例如如果将正则表达式检查或合并为单个模式,则为两个。

答案 2 :(得分:0)

  

public String group(int group)

返回上一个匹配操作期间给定组捕获的输入子序列。

问题是你没有在m2 matcher上调用find ...试试这个

m1.find() | m2.find()

您在运行时将matches()评估为false的事实是因为find()尝试查找与模式匹配的输入的子序列,而matches()评估整个输入。如果您希望您的示例与matches()一起使用,则应将模式更改为

Pattern p1 = Pattern.compile("^.*\\)\\s+(\\d+)\\(.*$");
Matcher m1 = p1.matcher(input);
Pattern p2 = Pattern.compile("^.*\\((\\d+)\\)\\s+bytes.*$");
Matcher m2 = p2.matcher(input);
...
if(m1.matches() && m2.matches())
//then store values of each matcher group.

您甚至可以将2种模式重组为1种单一模式,将表达式p1和p2与AND条件绑定...

 Pattern p1 = Pattern.compile("(?=^.*\\)\\s+(\\d+)\\(.*$)(?=^.*\\((\\d+)\\)\\s+bytes.*$).*$");

希望这会帮助你交配:)