我的matcher.groupCount()
給了我4個,但是當我使用,,...,matcher.group(0)
時,它給了我一個錯誤。模式/匹配器Java,非零組計數但錯誤檢索?
以下是我的代碼:
Pattern pattern = Pattern.compile("([0-9]+).([0-9]+).([0-9]+).([0-9]+)");
Matcher matcher1, matcher2;
GeoIP[0][0] = (GeoIP[0][0]).trim();
GeoIP[0][1] = (GeoIP[0][1]).trim();
System.out.println(GeoIP[0][0]);
System.out.println(GeoIP[0][1]);
matcher1 = pattern.matcher(GeoIP[0][0]);
matcher2 = pattern.matcher(GeoIP[0][1]);
System.out.println("matcher1.groupCount() = " + matcher1.groupCount());
System.out.println("matcher2.groupCount() = " + matcher2.groupCount());
System.out.println("matcher1.group(0) = " (matcher1.group(0)).toString());
控制檯:
Exception in thread "main" 1.0.0.0
1.0.0.255
matcher1.groupCount() = 4
matcher2.groupCount() = 4
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at filename.main(filename.java:linenumber)
行號指向
System.out.println("matcher1.group(0) = " (matcher1.group(0)).toString());
YESSSS!非常感謝!!!! <3 – FailedMathematician