【发布时间】:2010-08-23 20:40:40
【问题描述】:
$text_expression = 'word1 word2 "phrase 1" "phrase 2" -word3 -word4 -"phrase \"hello\" 3" -"phrase 4"';
我想搜索包含(word1 OR word2 OR 'phrase 1' OR 'phrase 2')且不包含(word3 OR word4 OR 'phrase "hello" 3' OR 'phrase 4')的字符串
与上面的 $text_expression 等效的正则表达式是什么?它产生一个像;
[包含] => 数组 (
[0] => word1
[1] => word2
[2] => phrase 1
) [不包含] => 数组 (
[0] => word3
[1] => word4
[2] => phrase "hello" 3
)
ps:如果要使字符串更容易,我可以用另一种方式来表述字符串(例如,使用其他字符而不是引号和破折号)
【问题讨论】:
-
请不要以为我只是问 b/c 我很懒...我尝试了 50 种其他变体,但在那些丑陋的字符之间发现了一些不起作用的东西。
-
在 Perl 中,由于正则表达式中的代码断言,这会更容易。您可以(在 Perl 中)设置例如在匹配期间标记变量并在匹配后简单地测试变量:/ ... word1(?{ ++$word_1; }) .../,但这在 PHP 中不起作用。所以看起来你必须对字符串进行多次传递。