[英]Java Regex matcher showing unexpected results
我試圖使用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();
}
}
問題是此塊:
if(m1.find() || m2.find()){
GroupBytes[0] = m1.group(1);
GroupBytes[1] = m2.group(1);
return GroupBytes;
}
由於您要輸入if
條件,如果匹配器m1
或m2
任何匹配成功,但是在執行m2.group(1)
時它將拋出IllegalStateException
因為由於||
而導致m2.find()
從未執行過 和m1.find()
返回true。
將該塊更改為使用&&
代替||
:
if(m1.find() && m2.find()){
GroupBytes[0] = m1.group(1);
GroupBytes[1] = m2.group(1);
return GroupBytes;
}
else
throw new BytesNotFoundException();
現在,代碼將為每個匹配器對象m1
和m2
執行find()
,然后為每個匹配器對象調用.group(1)
。
您正在做m1.find() || m2.find()
m1.find() || m2.find()
因此如果m1找到匹配項,將不會嘗試m2,因此您應該將其更改為例如檢查或將正則表達式合並為單個模式的兩個。
公共字符串組(int組)
返回在上一個匹配操作期間給定組捕獲的輸入子序列。
問題是您沒有在m2匹配器上調用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個單個模式,用AND條件將兩個表達式p1和p2綁定在一起。
Pattern p1 = Pattern.compile("(?=^.*\\)\\s+(\\d+)\\(.*$)(?=^.*\\((\\d+)\\)\\s+bytes.*$).*$");
希望這可以幫助您交配:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.