正则表达式 - 值不等于

时间:2021-04-21 22:34:41

Can you advise on a regular expression for:

你能建议正则表达式:

NOT EQUAL TO string1, string2, string3 and case insensitive

NOT EQUAL对string1,string2,string3和不区分大小写

I have the following, but not giving me what I want.

我有以下,但没有给我我想要的东西。

/^(?!string1|string2|string3)$/

Thanks

谢谢

4 个解决方案

#1


2  

Sorry if I'm on the wrong path, but why not negate the positive match?

对不起,如果我走错路,但为什么不否定积极的匹配呢?

if (!preg_match("/^(string1|string2|string3)$/i",$str) {
    // ...
}

#2


1  

You would need an AND in regex, not OR. Because if your string was string3, it will be unequal to string1 and string2, so it would match. You cannot solve this with regular expressions (at least not in a way that would be readable I assume ;)). Of course you can by inverting the output of the match.

你需要一个正则表达式的AND,而不是OR。因为如果你的字符串是string3,它将不等于string1和string2,所以它会匹配。你不能用正则表达式来解决这个问题(至少不是我认为可读的方式;))。当然你可以通过反转匹配的输出。

You could also use a non-regex approach (might be easier to maintain):

您还可以使用非正则表达式方法(可能更容易维护):

$blacklist = array('string1', 'string2', 'string3');

if(!in_array(strtolower($str), $blacklist)) {

}

#3


0  

Can you try this? if( !preg_match( '/(string1|string2|string3)/i', $str ) )

你能试试吗? if(!preg_match('/(string1 | string2 | string3)/ i',$ str))

#4


0  

I use this when I need to find "a file that does not contain..." :

当我需要找到“不包含......的文件”时,我会使用它:

(?s)\A((?!Copyright).)*\Z

(?S)\ A((?!版权所有)。)* \ž

This is for the string "Copyright"... replace the string as needed; as well as the \A (beginning of a file) and \Z (end of a file).

这是字符串“Copyright”...根据需要替换字符串;以及\ A(文件的开头)和\ Z(文件的结尾)。

#1


2  

Sorry if I'm on the wrong path, but why not negate the positive match?

对不起,如果我走错路,但为什么不否定积极的匹配呢?

if (!preg_match("/^(string1|string2|string3)$/i",$str) {
    // ...
}

#2


1  

You would need an AND in regex, not OR. Because if your string was string3, it will be unequal to string1 and string2, so it would match. You cannot solve this with regular expressions (at least not in a way that would be readable I assume ;)). Of course you can by inverting the output of the match.

你需要一个正则表达式的AND,而不是OR。因为如果你的字符串是string3,它将不等于string1和string2,所以它会匹配。你不能用正则表达式来解决这个问题(至少不是我认为可读的方式;))。当然你可以通过反转匹配的输出。

You could also use a non-regex approach (might be easier to maintain):

您还可以使用非正则表达式方法(可能更容易维护):

$blacklist = array('string1', 'string2', 'string3');

if(!in_array(strtolower($str), $blacklist)) {

}

#3


0  

Can you try this? if( !preg_match( '/(string1|string2|string3)/i', $str ) )

你能试试吗? if(!preg_match('/(string1 | string2 | string3)/ i',$ str))

#4


0  

I use this when I need to find "a file that does not contain..." :

当我需要找到“不包含......的文件”时,我会使用它:

(?s)\A((?!Copyright).)*\Z

(?S)\ A((?!版权所有)。)* \ž

This is for the string "Copyright"... replace the string as needed; as well as the \A (beginning of a file) and \Z (end of a file).

这是字符串“Copyright”...根据需要替换字符串;以及\ A(文件的开头)和\ Z(文件的结尾)。