How to check that a string does not contain "bird" or "cat" with a regular expression?
Please don't tell me about use the boolean !
to check it开发者_JAVA百科. Because I want to use a jquery plugin to do something in my function. And I want to fix it only regex section... Is that possible?
Better, IMO:
preg_match('/^(?!.*(cat|bird).*$)(......)/xs', $string);
You can use this assertion/negative assertion construct to check that the whole string does not contain bird
or cat
:
preg_match('/(?=^((?!cat|bird).)+$) (......)/xs', $string);
I assume it is not particularly efficient. But at least the asssertion is independent from the actual match regex - as example (......)
here.
I believe this is more simple and less confusing;
$string = ", user, test, me";
if (!preg_match("#(.*?)(cat|bird)(.*?)#", $string)) {
echo "yes, there is no cat or bird";
}
else{
echo "there is cat or bird";
}
I would recommend you to check it here http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html
精彩评论