【问题标题】:php preg_match - multivalue patternphp preg_match - 多值模式
【发布时间】:2013-09-12 07:16:05
【问题描述】:

iam 使用 preg_match 如下:

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

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

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

我尝试了否定 - 模式也是如此

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

或

$pattern="(Jim|goes|with){1}" 

出错或

$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 作为模式?

谢谢 - 我希望它

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

e1 i: (Jim){1}(goes){1}(with){1}
e1 o: no result

e2 i: (Jim|goes|with)
e2 o: two matches  "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"


e3 i: ^(^Jim|^goes|^with)
e3 o: two matches  "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"

哪个输入解决方案带有一个结果?

哪个解决方案的输入:“Jim goes with”会生成一个结果,例如:“Jim Carrey got crazy with santa Clause”它表示正则表达式中的一个和条件 - 但有可能吗?

解决方案:

 $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>" ;

输出:

我的搜索如下:Jim 和

我的模式如下:(Jim|goes|with)

preg_match 计数:2 =搜索计数:3

preg_match 计数:3 =搜索计数:3

array(1) {
   [0] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}

并与:

     $patternsearch="Jim goes  ";

我的搜索如下:Jim go

我的模式如下:(Jim|goes)

preg_match 计数:2 =搜索计数:2

preg_match 计数:2 =搜索计数:2

array(2) {
   [0] =>
   string(21) "Jim Carrey goes crazy"
   [1] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}

【问题讨论】:

标签: php regex preg-match multivalue and-operator


【解决方案1】:

嘿,

如何使用 preg_match_all (http://php.net/manual/en/function.preg-match-all.php) 和 $pattern="(Jim|goes|with)";

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

【讨论】:

  • 我们开始了:是的,这是最后的方法......解决方案已发布在主帖中
【解决方案2】:

我认为这里对如何使用正则表达式有些困惑。如果你使用/Jim|goes|with/,这意味着你想匹配任何包含Jim、goes、with的字符串。如果您还用括号括住您的正则表达式,即/(Jim|goes|with)/,您也会捕获匹配项。现在,由于您要匹配的两个句子都有Jim 和goes,所以您总是有一个肯定匹配。

现在,如果您想对包含所有单词Jim、“goes”和“with”的句子进行肯定匹配,您需要类似:

/\bJim\b.*?\bgoes\b.*?\bwith/

其中 '\b' 表示单词边界,避免与“Jimmy”、“mangoes”或“without”之类的错误匹配,. 或多或少表示“任何字符”(存在与换行符)和*? 是一个不情愿的量词,在这里Greedy vs. Reluctant vs. Possessive Quantifiers 有很好的解释。

我强烈建议您至少在这里 (http://en.wikipedia.org/wiki/Regular_expression) 看看一些非常基本的信息。

【讨论】:

    【解决方案3】:

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

    $pattern = '(Jim)?.*(goes)?.*(crazy)?';
    if (preg_match("/$pattern/is", $_search[0], $match)) {
        $num_matches = count(array_filter($match)) - 1;
    }
    

    array_filter 将删除空匹配,当未找到可选字符串时会发生这种情况。然后我们从计数中减去 1,因为 $match[0] 是整个 regexp 的匹配项,你不需要关心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多