I am referring a regular expression cheat sheet. It says
{3,5}
means 3,4,and 5
{3,5}?
means 3,4,5
ungreedy +
what does ungreedy +
indicate?
You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".
Greedy: As Many As Possible (longest match) By default, a quantifier tells the engine to match as many instances of its quantified token or subpattern as possible. This behavior is called greedy. For instance, take the + quantifier.
Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.
The quanitifier {3,5}?
means that it will try to match 3 occurrences first, then see if the rest of the expression matches. If the rest of the expression fails it will backtrack and try 4, then finally 5.
The greedy version {3,5}
will try the matches in the opposite order - longest first.
Note that greediness does not affect whether or not a string matches. It only affects the order in which the engine performs the search, and the contents of the captures if there are capturing groups.
Here's an example that demonstrates the difference. Imagine you have the string aaaaabc
.
(a{3,5})(\w*)
will capture aaaaa
and bc
. (rubular)
(a{3,5}?)(\w*)
will capture aaa
and aabc
. (rubular)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With