PHP 正则表达式不包含以下任何单词


php regex does not contain any of the following words

Im使用以下正则表达式检查字符串是否包含以下任何单词:

/(work|hello|yes)/

我如何反转它,以检查字符串是否不包含以下任何单词?

if (preg_match('/(work|hello|yes)/', trim(strtolower($mystring)))) {
}

注意 我不想使用!preg_match

可以使用负 looakhead 来匹配字符串,如果它不包含以下单词:

^(?!.*?(?:work|hello|yes)).*

还可能希望在单词之前/之后添加'b单词边界。

在 regex101.com 进行测试


如果是多行输入,请使用s (PCRE_DOTALL) 标志,使.也匹配换行符。

试试这个:

 $match    = '/work|hello|yes/';
 $myString =  trim(strtolower($mystring));
 if (preg_match($match,$myString)) {
 }

您错过了preg_match的第一个参数'

$mystring='hello world';
if (preg_match('/(work|hello|yes)/', trim(strtolower($mystring)))) {
    echo 'Yes'; //Result is yes
}else{
    echo 'No';
}