fork(1) download
  1. import re
  2. profanity_list = ['foo', 'bar']
  3. whitelist = ["BAR", "foo good"]
  4. pattern_profanity = re.compile(
  5. r'\b(?!(?:{})\b)(?i:{})\b'.format('|'.join(whitelist),'|'.join(profanity_list))) # same as r'\b{foo|bar}\b'
  6. s = 'foo BAR foo good Bar'
  7. censor_char = '*'
  8. print(pattern_profanity.pattern)
  9. print( re.sub(pattern_profanity, lambda m: censor_char*len(m.group(0)), s) )
  10. #https://r...content-available-to-author-only...1.com/r/fPFqoM/1
Success #stdin #stdout 0.03s 9564KB
stdin
Standard input is empty
stdout
\b(?!(?:BAR|foo good)\b)(?i:foo|bar)\b
*** BAR foo good ***