且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

分隔符之间的匹配文本:贪婪还是懒惰的正则表达式?

更新时间:2023-02-17 22:56:48

一些优点:

[^>]*:

  • 更具表现力.
  • 捕获换行符而与/s标志无关.
  • 考虑更快,因为引擎不必回溯即可找到成功的匹配项(使用[^>]引擎不会做出选择-我们仅提供一种将模式与字符串进行匹配的方法).
  • li>
  • 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).

.*?

  • 没有代码重复"-结束字符仅出现一次.
  • 在结束定界符超过一个字符长的情况下更简单. (在这种情况下,字符类将不起作用)常见的替代方法是(?:(?!END).)*.如果END分隔符是另一种模式,则情况更糟.
  • 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.