Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

operators - How does the ? make a quantifier lazy in regex

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.Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

? 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) (?<foo>...): 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 ?.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...