Is “zero or one” used for lazy assessment, i.e. Double Regular Expression Question Marks?

I am currently reading The Regular Expression Cookbook by Jan Goewaerts and Stephen Levitan. They mention that any quantifier can be made lazy by placing ?after it, so this includes a quantifier ?, resulting in ??. Unfortunately, the book is no longer mentioned ??.

As I understand it, it x?will match the x character zero or once, but preferably once. x??coincides with x equal to zero or the same, but will preferably correspond to zero time at which this is possible.

I played a little at regex101.com , but did not come up with a situation where this difference really mattered. Does it ??have any practical significance in any flavor of regular expression?

+4
source share
1 answer

Take for example the input x123

Now test with regex:

(x??)(x*\d+)

You will receive agreed groups:

  • ""
  • "x123"

Now test with regex:

(x?)(x*\d+)

You will receive agreed groups:

  • "x"
  • "123"

RegEx Demo for Test Case # 1 RegEx Demo for Test Case # 2

A-priory:

  • x??matches the character x from zero to one time, as little as possible, expanding as necessary [lazy]
  • x?matches the character x from zero to once, as many times as possible, returning as necessary [greedy]
+2

All Articles