Suppose I have string Heeello. Can someone tell my why the regex /H(e+)llo/ and the regex /H(e+?)llo/ return the same group eee although first is greedy and the second is lazy (so it should return e)?
-
1Still waiting for a more detailed answer, or could you please accept the best answer?Wiktor Stribiżew– Wiktor Stribiżew2016-07-27 09:24:27 +00:00Commented Jul 27, 2016 at 9:24
2 Answers
lazy is based on the following pattern. H(e+?)llo, here the following character is l, so inorder to find a match it would match all the e's until the l is reached.
Just remove the following llo from the above regex. Now it would capture the first e only. Since we removed the following pattern llo (ie, the pattern next to the lazy quantifier), it must capture the first e.
Consider Heeee as the input string and H(e+?) as the regex pattern.
H matches the letter H and e+? will do a non-greedy match of one or more e's. So this would match only He, because it finds the match after the first attempt. Here there is no following pattern.
Consider Heeeello as the input string and H(e+?)llo as the regex pattern.
Here H matches the first H and at first e+? matches the first e. Because the pattern is not complete yet, the regex engine picks up the third next pattern that is, l. In-order to find a match, regex engine matches all the e's upto the letter l .
These two regexes will do the same job. You dont need to use the non-greedy form.
^.*$
^.*?$
2 Comments
.+ is greedy which matches any character as much as possible.