我正在嘗試學習Java正則表達式。我想將幾個捕獲組(即j(a(va))
)與另一個字符串(即this is java. this is ava, this is va
)匹配。我期待的輸出爲:如何匹配多個捕獲組,但結果不如預期
I found the text "java" starting at index 8 and ending at index 12.
I found the text "ava" starting at index 21 and ending at index 24.
I found the text "va" starting at index 34 and ending at index 36.
Number of group: 2
但是,IDE而只輸出:
I found the text "java" starting at index 8 and ending at index 12.
Number of group: 2
爲什麼會出現這種情況?有什麼我失蹤?
原始代碼:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter your regex:");
Pattern pattern
= Pattern.compile(br.readLine());
System.out.println("\nEnter input string to search:");
Matcher matcher
= pattern.matcher(br.readLine());
boolean found = false;
while (matcher.find()) {
System.out.format("I found the text"
+ " \"%s\" starting at "
+ "index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
System.out.println("Number of group: " + matcher.groupCount());
}
if (!found) {
System.out.println("No match found.");
}
運行上面的代碼後,我已經進入了下輸入:
Enter your regex:
j(a(va))
Enter input string to search:
this is java. this is ava, this is va
而且IDE輸出:
I found the text "java" starting at index 8 and ending at index 12.
Number of group: 2
嘗試使用https://regex101.com/ –
我想你誤會什麼捕獲組辦。它們不會使正則表達式的其他部分成爲可選項,所以您的正則表達式只匹配整個字符串'java'。 – Barmar
請不要發佈從System.in'讀取的問題,並對結果進行一些處理,因爲這意味着您可以a)輕鬆地調試代碼以從System.in中讀取或從硬編碼中識別錯誤字符串。在這兩種情況下,這意味着代碼不是最簡單的例子,並且/或者錯誤的來源可以很容易地縮小。這也意味着更多的工作來重現問題。 – fabian