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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题