|
4 # 大 中 小 发表于 2008-04-22 12:30:56
Re:EL的贪婪、勉强和侵占量词
引用 教程里的代码:
我的理解:因为贪婪是从右到左,所以先从左边开始匹配,当找到foo的时候,找到了第一个匹配,所以把整个xfooxxxxxxfoo认为是匹配的字符串吗?
Enter your regex: .*foo // 贪婪量词
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
我的理解:从左到右,开始第一个找到匹配xfoo,然后会从截掉这个匹配的字符串里继续找匹配,对吗?
Enter your regex: .*?foo // 勉强量词
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.
还是不是很明白:
原话:第三个例子的量词是侵占,所以在寻找匹配时失败了。在这种情况下,整个输入的字符串被.*+消耗了,什么都没有剩下来满足表达式末尾的“foo”。
这里的[整个输入的字符串被.*+消耗了]应该怎么理解?
Enter your regex: .*+foo // 侵占量词
Enter input string to search: xfooxxxxxxfoo
No match found.
麻烦你了~
Enjoy Java,Enjoy eceryday !!!
|