Enter your regex: a?
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "a" starting at index 5 and ending at index 6.
I found the text "a" starting at index 6 and ending at index 7.
I found the text "a" starting at index 7 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.
Enter your regex: a*
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.
Enter your regex: a+
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.
上面的例子来自
Java Documentation
于量词。根据我从上面给出的示例中了解到的情况,以下是贪婪量词的工作原理:
? 和 *
将寻找提到的字符的存在。如果未找到它们前面的字符,则将该位置标记为零长度匹配。
*
一个字符一个字符的匹配,而?
一个组。
+
+
将进行正则表达式的逐组匹配。它不会进行零长度匹配。
我的问题是:
我对量词的理解是正确的还是我搞砸了?
此外,是否有任何对大脑友好的 Java 正则表达式指南(Google 上没有针对初学者的可用指南)?