How does the ? make a quantifier lazy in regex

后端 未结 4 1542
忘了有多久
忘了有多久 2020-12-06 07:56

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

4条回答
  •  猫巷女王i
    2020-12-06 08:54

    ? can mean a lot of different things in different contexts.

    • Following a normal regex token (a character, a shorthand, a character class, a group...), it means "Match the previous item 0-1 times".
    • Following a quantifier like ?, *, +, {n,m}, it takes on a different meaning: "Make the previous quantifier lazy instead of greedy (if that's the default; that can be changed, though - for example in PHP, the /U modifier makes all quantifiers lazy by default, so the additional ? makes them greedy).
    • Right after an opening parenthesis, it marks the start of a special construct like for example

      a) (?s): mode modifiers ("turn on dotall mode")
      b) (?:...): make the group non-capturing
      c) (?=...) or (?!...): lookahead assertion
      d) (?<=...) or (?: lookbehind assertion
      e) (?>...): atomic group
      f) (?...): named capturing group
      g) (?#comment): inline comments, ignored by the regex engine
      h) (?(?=if)then|else): conditionals

    and others. Not all constructs are available in all regex flavors.

    • Within a character class ([?]), it simply matches a verbatim ?.

提交回复
热议问题