This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Enter your regex: .*+foo // possessive quantifier
Enter input string to search: xfooxxxxxxfoo
No match found.
why we got No match found here?
Because of the way the possessive modifier works. The possessive modifier acts like the greedy modifier, except that it doesn't back off. So...
The ".*+" portion will match everything, just like a greedy modifier. However, if it does that, while the ".*+" portion will match, the overall match will fail since there isn't anything left for the "foo" portion of the regex to match. In the case of the greedy modifier, it will backtrack, and match less, in order to allow the overall match to work.
Unfortunately, in the case of the possessive modifier, there isn't a backtrack, and hence, the overall match will fail. This is why you have to be really careful with the posessive modifier -- only use it for cases where you can perform the match correctly without a backtrack, because it won't.