1

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)?

1
  • 1
    Still waiting for a more detailed answer, or could you please accept the best answer? Commented Jul 27, 2016 at 9:24

2 Answers 2

1

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 .

DEMO

These two regexes will do the same job. You dont need to use the non-greedy form.

^.*$
^.*?$
Sign up to request clarification or add additional context in comments.

2 Comments

So when the pattern doesn't end with a quantifier, this quantifier will never work as lazy?
yep, .+ is greedy which matches any character as much as possible.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.