dsirr48088 2016-05-06 22:41
浏览 42
已采纳

模式适用于regex101,但不适用于服务器

My pattern:

/{LOOP:(\w+)[^}]*}((\s*?.?)*?){\/LOOP:\1}/

My test string:

<tbody>
    {LOOP:MENU}
    <tr>
        <td>{VAR:ID}</td>
        <td>{VAR:NAME}</td>
        <td>{VAR:ROLLE}</td>
        <td>{VAR:ACTIONS}</td>
    </tr>
    {/LOOP:MENU}
</tbody>

When you try it on regex101 it is working without any problems. But when I try it on my Server with preg_match_all() it is not working. preg_last_error() returns:

2(PREG_BACKTRACK_LIMIT_ERROR).

How is that possible with regex101 telling me "1 Match in 581 Steps"? Or has the number of Steps nothing to do with Backtrack?

I'm using apache2 Server with PHP Version 5.5.9-1ubuntu4.16.

In my php.ini, pcre.backtrack_limit is set on 1000000.

EDIT:

Following the tip of Alan Moore, I now use the following pattern:

/{LOOP:(\w+)[^}]*}(.*?){\/LOOP:\1}/s

This gives back 0(NO_ERROR). But on my server it is now not matching anything. Why is it working on regex101 but not on my server?

PHP:

$test="<tbody>
        {LOOP:MENU}
        <tr>
            <td>{VAR:ID}</td>
            <td>{VAR:NAME}</td>
            <td>{VAR:ROLLE}</td>
            <td>{VAR:ACTIONS}</td>
        </tr>
        {/LOOP:MENU}
       </tbody>"
preg_match_all("/{LOOP:(\w+)[^}]*}(.*?){\/LOOP:\1}/s", $test, $matches, PREG_SET_ORDER);
// $matches is now an emty array.
  • 写回答

2条回答 默认 最新

  • dsh1956 2016-05-08 05:52
    关注

    php needs an extra backslash here and there in regexes. in this case, changing \1 to \\1 works:

    preg_match_all("/{LOOP:(\w+)[^}]*}(.*){\/LOOP:(\\1)}/s", ...
    

    alternatively, single backslash is ok here with single-quotes, and you can save some trouble by always using single quotes around regexes:

    preg_match_all('/{LOOP:(\w+)[^}]*}(.*){\/LOOP:(\1)}/s', ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog