Forums Register Login

Regarding Regular Expressions

+Pie Number of slices to send: Send
Hi Friends,

can you please explain What is differences among Greedy,Reluctant, and Possesive Quantifiers with example?
Actually I'm confusing with these Quantifiers.
+Pie Number of slices to send: Send
Does this Java™ Tutorials link help?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1007 times.
Similar Threads
regex
Greedy and Reluctant Quantifiers
Quantifiers and Greedy Quantifiers
Regex Doubt
Greedy and Reluctant Quantifiers
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:21:03.