简体   繁体   中英

regular expression - value not equal to

Can you advise on a regular expression for:

NOT EQUAL TO string1, string2, string3 and case insensitive

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

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

Thanks

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

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

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.

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

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

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

}

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

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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM