您只需调用sub即可完成此操作:big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
the_message = big_regex.sub("repl-string", str(word[1]))
示例:>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
>>> the_message = big_regex.sub("", 'this message contains Some really Random Words')
>>> the_message
'this message contains really '
请注意,使用str.replace可能会导致细微的错误:>>> 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'
使用re.sub时会给出正确的结果:>>> 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'
正如thg435所指出的,如果要替换单词而不是每个子字符串,则可以将单词边界添加到regex中:big_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, words)))
这将取代'random words'中的'random',而不是'pseudorandom words'。