Friday, July 19, 2019

Importance of ? in Regex

It is the difference between greedy and non-greedy quantifiers.
Consider the input 101000000000100.
Using 1.*1* is greedy - it will match all the way to the end, and then backtrack until it can match 1, leaving you with 1010000000001.
.*? is non-greedy. * will match nothing, but then will try to match extra characters until it matches 1, eventually matching 101.
All quantifiers have a non-greedy mode: .*?.+?.{2,6}?, and even .??.
In your case, a similar pattern could be <([^>]*)> - matching anything but a greater-than sign (strictly speaking, it matches zero or more characters other than > in-between < and >).



No comments:

Post a Comment