How does the ? make a quantifier lazy in regex

Posted by Uriel Katz on Programmers See other posts from Programmers or by Uriel Katz
Published on 2013-07-01T08:33:42Z Indexed on 2013/07/01 10:28 UTC
Read the original article Hit count: 398

Filed under:
|

I've been looking into regex lately and figured that the ? operator makes the *,+, or ? lazy. My question is how does it do that?

Is it that *? for example is a special operator, or does the ? have an effect on the *? In other words, does regex recognize *? as one operator in itself, or does regex recognize *? as the two separate operators * and ??

If it is the case that *? is being recognized as two separate operators, how does the ? affect the * to make it lazy. If ? means that the * is optional, shouldn't this mean that the * doesn't have to exists at all. If so, then in a statement .*? wouldn't regex just match separate letters and the whole string instead of the shorter string?

Please explain, I'm desperate to understand.

© Programmers or respective owner

Related posts about operators

Related posts about regex