正则表达式检查数组是否包含一些单词

时间:2023-02-10 01:33:52

I have an array of matching words:

我有一系列匹配的单词:

$search = array("lorem","dolor","sit");

And an array to search in:

还有一个要搜索的数组:

 $results= array(
       "Lorem ipsum dolor sit amet, consectetur",
       "Ut enim ad minim veniam, quis nostrud exercitation"
       "Duis aute irure dolor in reprehenderit in voluptate velit esse"
       "Excepteur sint occaecat cupidatat non proident"
    );

Is there a regex to return true where two of given words are matching?

有两个给定的单词匹配时,是否有正则表达式返回true?

2 个解决方案

#1


1  

You could use a word boundary \b in your regular expression.

您可以在正则表达式中使用单词boundary \ b。

A word boundary is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character.

单词边界是\ w和\ W(非单词字符)之间的位置,或者如果字符串开始或结束(分别)带有单词字符,则位于字符串的开头或结尾。

So maybe something like this..

也许像这样......

foreach ($results as $result) {
   $pattern = "/\b(" . implode('|', $search) . ")\b/i";
   $found   = preg_match_all($pattern, $result, $matches);

   if ($found) {
     print_r($matches[0]);
   }
}

Or you could do away with your search array and just use it as a regular expression:

或者您可以取消搜索数组并将其用作正则表达式:

foreach ($results as $result) {
   $found = preg_match_all("/\b(?:lorem|dolor|sit)\b/i", $result, $matches);
   if ($found) {
     print_r($matches[0]);
   }
}

Output:

Array
(
    [0] => Lorem
    [1] => dolor
    [2] => sit
)
Array
(
    [0] => dolor
)

#2


0  

You can generate a regex to search by this

您可以生成一个正则表达式来搜索

$regex = '/(' . implode('|', $search) . ')/i';

which will be:

这将是:

/(lorem|dolor|sit)/i

The /i makes it caseless.

/ i使它无壳。

You can then use the return value of preg_match_all() to see the number of matched words.

然后,您可以使用preg_match_all()的返回值来查看匹配单词的数量。

#1


1  

You could use a word boundary \b in your regular expression.

您可以在正则表达式中使用单词boundary \ b。

A word boundary is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character.

单词边界是\ w和\ W(非单词字符)之间的位置,或者如果字符串开始或结束(分别)带有单词字符,则位于字符串的开头或结尾。

So maybe something like this..

也许像这样......

foreach ($results as $result) {
   $pattern = "/\b(" . implode('|', $search) . ")\b/i";
   $found   = preg_match_all($pattern, $result, $matches);

   if ($found) {
     print_r($matches[0]);
   }
}

Or you could do away with your search array and just use it as a regular expression:

或者您可以取消搜索数组并将其用作正则表达式:

foreach ($results as $result) {
   $found = preg_match_all("/\b(?:lorem|dolor|sit)\b/i", $result, $matches);
   if ($found) {
     print_r($matches[0]);
   }
}

Output:

Array
(
    [0] => Lorem
    [1] => dolor
    [2] => sit
)
Array
(
    [0] => dolor
)

#2


0  

You can generate a regex to search by this

您可以生成一个正则表达式来搜索

$regex = '/(' . implode('|', $search) . ')/i';

which will be:

这将是:

/(lorem|dolor|sit)/i

The /i makes it caseless.

/ i使它无壳。

You can then use the return value of preg_match_all() to see the number of matched words.

然后,您可以使用preg_match_all()的返回值来查看匹配单词的数量。