[英]PHP RegEx not matching a string that it should match
This is driving me insane...这让我发疯...
I have the following code:我有以下代码:
# open pdf
$pdf = file_get_contents('myfile.pdf');
echo("RE 1:\n");
preg_match('/^[0-9]+ 0 obj.*\/Contents \[ ([0-9]+ [0-9]+) R \\]/msU', $pdf, $m);
var_dump($m);
echo("\nRE 2:\n");
preg_match('/^8 0 obj.*\/Contents \[ ([0-9]+ [0-9]+) R \\]/msU', $pdf, $m);
var_dump($m);
The file myfile.pdf contains the following text:文件myfile.pdf包含以下文本:
...
8 0 obj
<<
/Type /Page
/Parent 2 0 R
/Resources 6 0 R
/Contents [ 5 0 R ]
>>
endobj
...
The only difference between those two regular expressions is the numeric range at the beginning of the string.这两个正则表达式之间的唯一区别是字符串开头的数字范围。 Yet I get the following output:但我得到以下输出:
RE 1:
array(0) {
}
RE 2:
array(2) {
[0]=>
string(78) "8 0 obj
<<
/Type /Page
/Parent 2 0 R
/Resources 6 0 R
/Contents [ 5 0 R ]"
[1]=>
string(3) "5 0"
}
I would expect both regular expressions to return similar results, but the regular expression with the numeric range at the start ( RE 1 ) doesn't return any results.我希望两个正则表达式都返回相似的结果,但是以数字范围开头的正则表达式 ( RE 1 ) 不返回任何结果。 Is this a bug or am I doing something wrong?这是一个错误还是我做错了什么?
After adding preg_last_error()
, I am getting PREG_BACKTRACK_LIMIT_ERROR
.添加preg_last_error()
,我收到PREG_BACKTRACK_LIMIT_ERROR
。 How can I fix that?我该如何解决?
I'm guessing that you might be designing an expression that would somewhat look like,我猜你可能正在设计一个看起来有点像的表达式,
[0-9]+\s+0\s+obj\b.*?\/Contents\s+\[\s*([0-9]+\s+[0-9]+)\s+R\s*\]
on s
mode.在s
模式。
$re = '/[0-9]+\s+0\s+obj\b.*?\/Contents\s+\[\s*([0-9]+\s+[0-9]+)\s+R\s*\]/s';
$str = '8 0 obj
<<
/Type /Page
/Parent 2 0 R
/Resources 6 0 R
/Contents [ 5 0 R ]
>>
endobj
8 0 obj
<<
/Type /Page
/Parent 2 0 R
/Resources 6 0 R
/Contents [ 5 0 R ]
>>
endobj';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
The expression is explained on the top right panel of regex101.com , if you wish to explore/simplify/modify it, and in this link , you can watch how it would match against some sample inputs, if you like.该表达式在regex101.com 的右上角面板中进行了解释,如果您希望探索/简化/修改它,并且在此链接中,您可以观看它如何与某些示例输入匹配,如果您愿意的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.