0

Can I know the difference between .? and .??. I don't quite see any difference.Thanks for the help.

Tibrogargan
  • 3,599
  • 3
  • 16
  • 33
  • I'm not really sure what you're asking. Have you tried matching using those same patterns? If so, can you post the code that you've tried, your inputs, what you expected the outputs would be? – Wayne Werner Apr 17 '16 at 05:02
  • sure. I checked the regex "l.?z" and "l.??z" in lazzzzzzzzz . They both match till laz, where as "l.?z" matches lzz in "lzzzzzzzzz" and only "lz" in case of l.??z. Thank you. – the_dark_knight Apr 17 '16 at 05:10

1 Answers1

0

.? is greedy (consumes as many matches as possible), .?? is lazy (consumes as few matches as possible)

i.e For the string lzzzz, l.?z matches "lzz" (the .? consumes a z) but l.??z matches "lz" (the .?? consumes nothing)

Tibrogargan
  • 3,599
  • 3
  • 16
  • 33
  • See this for an explanation of the difference: http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions – Tibrogargan Apr 17 '16 at 05:07
  • Hi Tibro! But isn't a "?" used to stop being greedy? – the_dark_knight Apr 17 '16 at 05:18
  • @Sameer_184 ? Just means "the previous expression is optional". Being greedy means "consume if you can" .. so it did. lazy means "don't consume if you don't need to, so `.??` doesn't. The `l` and `z` are still required, but `.??` matched nothing. – Tibrogargan Apr 17 '16 at 05:22
  • @Sameer_184 only quantifiers can be greedy or lazy..You are saying that `?` is used for laziness..but in your statement `.` itself is not a quantifier(so neither greedy nor lazy), the next term is `?` which is a quantifier..So, the whole term `.?` is greedy or can be made lazy using another `?` and finally as `.??`..You can understand with the analogy that `.*` is greedy but is made lazy using `.*?` – rock321987 Apr 17 '16 at 05:33
  • `?` is not used for laziness, it's the greedy version of "previous expression matches zero or one times" (i.e. will prefer to match one time if possible). `??` is the lazy version of "previous expression matches zero or one times" (i.e. will prefer to match zero times if possible) – Tibrogargan Apr 17 '16 at 05:34