假设是,或和是是随机单词(前提是两个是相同),仅在此处表示:
我正在尝试使用正则表达式在任何地方匹配 YES , OR 和 YES ,条件是 只有在字符串为2的情况下,字符串才必须匹配。 。
因此,如果我尝试使用以下测试字符串:
yes or yes
no or yes
yes or no
maybe yes or yes
maybe yes or no
yes maybe or yes
maybe yes maybe yes maybe or
它只能匹配以下内容:
yes or yes
maybe yes or yes
yes maybe or yes
maybe yes maybe yes maybe or
这是我正在处理的正则表达式查询:
^(?=.*(?=\w.*yes)(?=\w.*or)(?=\w.*yes)).*
很遗憾,我的查询仍然与no or yes
相匹配。
这是我的正则表达式链接:Click Here
答案 0 :(得分:0)
答案 1 :(得分:0)
使用substr_count()
<?php
$string = 'yes or yes
no or yes
yes or no
maybe yes or yes
maybe yes or no
yes maybe or yes
maybe yes maybe yes maybe or';
$array = explode("\n", $string);
$expected = [];
//print_r($array);
foreach($array as $line){
$words = substr_count($line,'yes');
if($words==2 && strpos($line, 'or') !== false ){
$expected[]= $line;
}
}
echo implode("\n",$expected);
输出:
yes or yes
maybe yes or yes
yes maybe or yes
maybe yes maybe yes maybe or