This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Regular Expressions

 
Raj chiru
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

can you please explain What is differences among Greedy,Reluctant, and Possesive Quantifiers with example?
Actually I'm confusing with these Quantifiers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this Java™ Tutorials link help?
 
Raj chiru
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi campbell,
Thanks For Your Reply,


Enter your regex: .*+foo // possessive quantifier
Enter input string to search: xfooxxxxxxfoo
No match found.


why we got No match found here?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj chiru wrote:
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.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic