Search code examples
phpregexwhitelistblacklist

bad-good words with regular expression


I have some list:

$bad_words = ['aaa', 'bbb', 'ccc']
$good_words= ['__aaa', 'bbb==', '#ccc==']

By this lists I'd generate these rules:

$rules = ['/((?<!__)aaa)/', '/(bbb(?!==))/', '/((?<!#)ccc(?!==))/']

Problem: the ccc-rule is not correct (https://regex101.com/r/cC3hY7/1): it must to find strings like #ccc, #ccc-- or ccc== and exclude only strings like #ccc==

How to fix it?


Solution

  • You could simply use (*SKIP)(*F) like below.

        (?:#ccc==|bbb==|__aaa)(*SKIP)(*F)|(?:aaa|bbb|ccc)
    #   |<- Strings you don't want ----->|<--strings you want-->
    

    DEMO