Matching text between delimiters: greedy or lazy regular expression?

后端 未结 3 598
时光说笑
时光说笑 2021-02-01 21:34

For the common problem of matching text between delimiters (e.g. < and >), there\'s two common patterns:

  • using the greedy *
3条回答
  •  無奈伤痛
    2021-02-01 21:42

    Some advantages:

    [^>]*:

    • More expressive.
    • Captures newlines regardless of /s flag.
    • Considered quicker, because the engine doesn't have to backtracks to find a successful match (with [^>] the engine doesn't make choices - we give it only one way to match the pattern against the string).

    .*?

    • No "code duplication" - the end character only appears once.
    • Simpler in cases the end delimiter is more than a character long. (a character class would not work in this case) A common alternative is (?:(?!END).)*. This is even worse if the END delimiter is another pattern.

提交回复
热议问题