Reputation: 11
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
)?
Upvotes: 1
Views: 142
Reputation: 3929
The lazy regex try to find a match with the less effort possible. If you would have to match heeee with /H(e+?)/, you would get just he. And with /H(e+)/, heeee.
But as the sequence of e have to be followed by llo, even the lazy regex have to capture all the e to find llo.
Upvotes: 0
Reputation: 174844
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.
^.*$
^.*?$
Upvotes: 1