php预匹配-多值模式

php预匹配-多值模式,php,regex,preg-match,multivalue,and-operator,Php,Regex,Preg Match,Multivalue,And Operator,iam使用preg_match,如下所示: 案例1:模式有两个词。使用preg match解决方案-它返回两个结果 案例2:模式有三个单词。使用preg match解决方案-它返回两个结果 在我看来,案例2将只返回一个结果。但我没有任何办法 我试着用否定——模式也是如此 $pattern="^(^Jim|^goes|^with)"; ) 或 出错或 $pattern="(Jim){1}(with){1}" 出错 如需解释: $pattern1="(Jim|goes|with)"; $_se

iam使用preg_match,如下所示:

案例1:模式有两个词。使用preg match解决方案-它返回两个结果

案例2:模式有三个单词。使用preg match解决方案-它返回两个结果

在我看来,案例2将只返回一个结果。但我没有任何办法

我试着用否定——模式也是如此

$pattern="^(^Jim|^goes|^with)"; ) 

出错或

$pattern="(Jim){1}(with){1}"
出错

如需解释:

$pattern1="(Jim|goes|with)";
$_search[0]="Jim Carrey goes crazy";
$_search[1]="Jim Carrey goes crazy with santa clause";

preg_match("/$pattern1/is",$_search[0] )
preg_match("/$pattern1/is",$_search[1] )
在我的示例中,是否可以为一个结果获取and as模式

谢谢-我希望如此

编辑:输入(i)-输出(o)示例(e)

哪种输入溶液有一个结果

输入哪个解决方案:“Jim goes with”生成一个结果,例如:“Jim Carrey疯狂地使用santa子句”,这在正则表达式中表示and条件,但这可能吗

解决方案:

 $patternsearch=chop("Jim goes with  ");

 if(preg_match('/ /',$patternsearch)){
 $_array_pattern = explode( ' ', $patternsearch );
 $text = preg_replace('/ /','|',$patternsearch); 
 $pattern1=''.'('.$text.')';
 }else
 {
 $pattern1 = $patternsearch;
 }

 echo "my search is as follow: $patternsearch"."</br>";
 echo "my pattern is as follow: $pattern1"."</br></br>";


foreach($_search as $search){

$andcounter= preg_match_all("/$pattern1/isx", $search,$matscha);

echo "preg_match count:  $andcounter =";
echo "search count :  ".count($_array_pattern)."</br></br>";

if(count($_array_pattern) === $andcounter ){

$item[]=$search;

}



} 
echo "<pre>";
var_dump($item);
echo "</pre>" ;
以及:

array(2) {
   [0] =>
   string(21) "Jim Carrey goes crazy"
   [1] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}
我的搜索结果如下:吉姆走了

我的模式如下:(吉姆走了)

预匹配计数:2=搜索计数:2

preg_match_all("/$pattern/is", $_search[0] ) // return 2
preg_match_all("/$pattern/is", $_search[1] ) // return 3
预匹配计数:2=搜索计数:2

preg_match_all("/$pattern/is", $_search[0] ) // return 2
preg_match_all("/$pattern/is", $_search[1] ) // return 3
嘿嘿,

使用preg_match_all()和$pattern=“(Jim | goes | with)”如何


我认为关于如何使用正则表达式这里有点混乱。如果使用
/Jim | goes | with/
,则表示要将包含
Jim
goes
的任何字符串匹配。如果您还用括号括住正则表达式,即
/(Jim | goes | with)/
,您还可以捕获匹配项。现在,由于您要匹配的两个句子都有
Jim
goes
,因此您总是有一个正匹配

现在,如果您想对包含所有单词的句子进行正匹配,
Jim
,“goes”和“with”,则需要如下内容:

$pattern = '(Jim)?.*(goes)?.*(crazy)?';
if (preg_match("/$pattern/is", $_search[0], $match)) {
    $num_matches = count(array_filter($match)) - 1;
}
其中“\b”表示单词边界,避免与“Jimmy”、“mangoes”或“without”等词进行错误匹配,
表示或多或少的“任何字符”(与换行符相关的微妙之处),
*?
是一个不情愿的量词,这里对此进行了很好的解释


我强烈建议您至少在这里()查看一些非常基本的信息。

我不完全确定我是否理解这个问题,但也许这就是您想要的:


array\u filter
将删除空匹配项,当找不到可选字符串时会出现这种情况。然后,我们从计数中减去1,因为
$match[0]
是整个regexp的匹配项,您不关心它。

显示输入和输出数据示例。“(^Jim | ^goes | ^with)”不是否定,因为
^
不在字符类中。我们开始了:是的,这是最后的方法。。。解决方案张贴在主帖子中
array(2) {
   [0] =>
   string(21) "Jim Carrey goes crazy"
   [1] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}
preg_match_all("/$pattern/is", $_search[0] ) // return 2
preg_match_all("/$pattern/is", $_search[1] ) // return 3
/\bJim\b.*?\bgoes\b.*?\bwith/
$pattern = '(Jim)?.*(goes)?.*(crazy)?';
if (preg_match("/$pattern/is", $_search[0], $match)) {
    $num_matches = count(array_filter($match)) - 1;
}