【发布时间】:2020-06-09 10:25:15
【问题描述】:
我尝试编写一个快速的正则表达式,但是当我测试它时,PHP (preg_replace_callback) 将有 houndres 的步骤来获得结果,我认为这不是一个好的性能 - 必须更快。
我的正则表达式代码:\{if\s{1}(.+?)\}\n(((?R)|.*?)+)\{\/if\}
代码,应该被解析(递归):
{if $name == 'Tree'}
Hey, this is a Tree!
{/if}
{if $name == 'Example'}
{if $number == '1'}
Hey, this is an Example with the number 1
{/if}
{/if}
您可以在 regex101 here 上测试示例。
有没有办法加快我的正则表达式的速度,还是我必须接受这个速度?
【问题讨论】:
-
\{if\s(.+?)}\R((?>(?!\{\/?if[}\s]).|(?R))*?)\{\/if}? -
是的,就像那样!它比我的正则表达式快一点!
标签: php regex preg-replace preg-replace-callback