nov/09
13
Remove profanity (or any words) with a regular expression
0 Comments | Tags: python regular-expressions snippets
If you have comments on your site, or allow use generated content to appear in prominent places, it's a good idea to remove profanity. This regular expression matches case-insensitive for any bad words, and strips the entire word to the leading and trailing whitespace, making it possible to specify "darn" and catch "goshdarn".
import re
def remove_bad_words(text, replace_text=''):
bad_words = ['poo', 'darn' ...] # you get the idea
return re.sub('(?i)[^\s]*(%s)[^\s]*' % '|'.join(bad_words), replace_text, text)
Comments (0)
Commenting has been disabled for this entry
