【问题标题】:PHP preg_match_all not working on large dataPHP preg_match_all 不适用于大数据
【发布时间】:2018-05-16 06:59:14
【问题描述】:

为什么preg_match_all 不起作用?

$pattern = '/\{(?:[^{}]|(?R))*\}/';

$result = 161240 个字符

       if (preg_match_all($pattern, $result, $matches)) {
            echo 'Success';
        } else {
            echo 'Not working';
        }

这显示:“不工作”

【问题讨论】:

标签: php laravel


【解决方案1】:

该模式使用正则表达式递归匹配平衡的大括号。模式本身看起来不错,可以用作 intended

<?php
$re = '/{(?>[^{}]|(?R))*\}/m';
$str = 'Why is preg_match_all not working?{{{{{
$pattern = \'/\\{(?:[^{}]|(?R))*\\}/\';
$result = 161240 characters
       if (preg_match_all($pattern, $result, $matches)){ {
            echo \'Success\';
        } else {
            echo \'Not working\';
        }
}}}}}}{}{}{}{}{}{}{}{{{{{{}}}}}';

//preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
if (preg_match_all($re, $str, $matches)) {
echo 'Success\n';
} else {
echo 'Not working\n';
}

// Print the entire match result
var_dump($matches);

这也适用于较大的输入(此处为 tested 约 5000 个字符)。 最可能的解释是:模式没有找到有效的匹配。

但是,您正在对一个非常大的输入字符串运行递归正则表达式。很多,事情可能会出错。 PCRE 引擎达到内部限制,您的字符串未正确编码,超时等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多