簡體   English   中英

正則表達式模式未獲取匹配項

[英]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)因為它返回第一個捕獲的字符串組。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM