添加括号时 preg_match 失败
以下是我正在使用的正则表达式的简化。在我的开发计算机上,$pattern1
和 $pattern2
返回匹配项,但是在我的生产计算机上,只有 $pattern1
返回匹配项!显然,$pattern1
和 $pattern2
之间的唯一区别是其中一个单词周围有括号。然而,两者都是有效的模式,应该与给定的干草堆匹配(据我所知)。
$pattern1 = '/\<a name="ERROR TEXT"\>\<\/a\>\s*?validated\s*?\<\/span\>\s*?\<\/h1\>/';
$pattern2 = '/\<a name="ERROR TEXT"\>\<\/a\>\s*?(validated)\s*?\<\/span\>\s*?\<\/h1\>/';
$haystack = '- IFCS msg value, BOOKMARKED AS ERROR TEXT -->
<a name="ERROR TEXT"></a>
validated</span>
</h1>
<!-- START: .formActionHolder -->
<div class="formActionHolder">';
preg_match($pattern1, $haystack, $matches);
print_r($matches);
以前有人发现过这个问题吗?请注意,这不是正则表达式的全部 - 这是一个简化版本,我已将其确定为问题所在。在我的实际代码中,“validated”的值不是一个常量 - 因此我使用括号来捕获该词的原因。当然,这些模式在括号内还有其他字符,以便我可以在这里捕获可变单词。这只是一个简化的示例,它解决了我在两个看似不错的正则表达式中遇到的问题。
在我的开发机器上,我使用 php5.3.2 和 PCRE 7.8 库,在我的生产机器上,我使用 php5.2.4 和 PCRE 7.4。
the following is a simplification of a regex i am using. on my development machine both $pattern1
and $pattern2
return a match, however on my production machine only $pattern1
returns a match! clearly the only difference between $pattern1
and $pattern2
is that one of them has brackets around a word. however both are valid patterns which should match the given haystack (as far as i know).
$pattern1 = '/\<a name="ERROR TEXT"\>\<\/a\>\s*?validated\s*?\<\/span\>\s*?\<\/h1\>/';
$pattern2 = '/\<a name="ERROR TEXT"\>\<\/a\>\s*?(validated)\s*?\<\/span\>\s*?\<\/h1\>/';
$haystack = '- IFCS msg value, BOOKMARKED AS ERROR TEXT -->
<a name="ERROR TEXT"></a>
validated</span>
</h1>
<!-- START: .formActionHolder -->
<div class="formActionHolder">';
preg_match($pattern1, $haystack, $matches);
print_r($matches);
has anyone found this problem before? note that this is not the whole of the regex - this is a simplified version which i have identified as being the problem. in my actual code, the value of 'validated' is not a constant - hence my reason for using brackets to capture the word. of course the patterns have other characters within the parenthesis as well so that i can capture the variable words here. this is just a simplified example which hones in on the problem that i am having with two seemingly fine regexes.
on my development machine i am using php5.3.2 with the pcre 7.8 library and on my production machine i am using php5.2.4 with pcre 7.4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
括号用于在 php 正则表达式中进行分组,除非您转义它们以使它们充当字符本身。
Parenthesis are used for grouping in a php regex and act as such unless you escape them to make them act as the characters themselves.
您确定 $pattern2 不匹配吗?在我的日食中,它匹配,显示
数组([0] => 已验证
[1] =>已验证)
are you sure the $pattern2 coundn't match? In my eclipse, it match, show
Array ( [0] => validated
[1] => validated )
我想到了
$pattern2
中的?(
组合,所以我删除了?
来制作并且有效!!它非常奇怪 - 可能甚至是一个错误?
所以看起来
?(validated)
位被解释为条件子模式,而不是使用问号来使\s*
不贪婪在我看来这不是正确的行为
啊……它。有点痛苦,因为现在我的
*
会很贪婪,但在这种情况下,正则表达式模式会满足我的要求......感谢您所有有用的评论!
i had a thought about the
?(
combination in$pattern2
so i removed the?
to makeand that works!! its very strange - possibly even a bug?
so it looks like the
?(validated)
bit was being interpreted as a conditional subpattern rather than the question mark being used to make the\s*
ungreedythat doesn't look like correct behavior to me.
ah well...its a bit of a pain since now my
*
will be greedy. the regex pattern does what i want in this instance though...thanks for all your helpful comments!