You can do this with a single sub call:
big_regex = re.compile('|'.join(map(re.escape, prohibitedWords))) the_message = big_regex.sub("repl-string", str(word[1]))
Example:
>>> import re >>> prohibitedWords = ['Some', 'Random', 'Words'] >>> big_regex = re.compile('|'.join(map(re.escape, prohibitedWords))) >>> the_message = big_regex.sub("<replaced>", 'this message contains Some really Random Words') >>> the_message 'this message contains <replaced> really <replaced> <replaced>'
Please note that using str.replace may lead to minor errors:
>>> words = ['random', 'words'] >>> text = 'a sample message with random words' >>> for word in words: ... text = text.replace(word, 'swords') ... >>> text 'a sample message with sswords swords'
when using re.sub gives the correct result:
>>> big_regex = re.compile('|'.join(map(re.escape, words))) >>> big_regex.sub("swords", 'a sample message with random words') 'a sample message with swords swords'
As thg435 points out, if you want to replace words, and not every substring, you can add word boundaries to the regular expression:
big_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, words)))
this would replace 'random' with 'random words' , but not with 'pseudorandom words' .
source share