简体   繁体   中英

Match regular expression not containing bad words

How to make a preg_match pass when the subject does not contain bad words? This is doing the inverse:

$pattern = '/\b(some|bad|words)\b/i';

function matches($pattern, $value) {
    echo "matches $pattern: $value, matches: " . preg_match($pattern, $value)."\n";
}

matches($pattern, "This should match");
matches($pattern, "This bad string should not match");

All examples I found so far do the opposite: they match when subject contains bad words.

I want it to match when it does not contain bad words.

Tried several combinations and searches, but could not find a solution. Using '?!' with '\\b' seems to be not possible?

^((?!\b(some|bad|words)\b).)*$

You can use this

^((?!some|bad|words).)*$

Demo

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