[英]Replace all words from word list with another string in python
I have a user entered string and I want to search it and replace any occurrences of a list of words with my replacement string. 我有一个用户输入的字符串,我想搜索它并用我的替换字符串替换任何出现的单词列表。
import re
prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
# word[1] contains the user entered message
themessage = str(word[1])
# would like to implement a foreach loop here but not sure how to do it in python
for themessage in prohibitedwords:
themessage = re.sub(prohibitedWords, "(I'm an idiot)", themessage)
print themessage
The above code doesn't work, I'm sure I don't understand how python for loops work. 上面的代码不起作用,我确定我不明白python for循环是如何工作的。
You can do that with a single call to sub
: 你可以通过一次调用
sub
来做到这一点:
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>'
Note that using str.replace
may lead to subtle bugs: 请注意,使用
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'
while using re.sub
gives the correct result: 使用
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'
As thg435 points out, if you want to replace words and not every substring you can add the word boundaries to the regex: 正如thg435指出的那样,如果你想要替换单词而不是每个子字符串,你可以将单词边界添加到正则表达式:
big_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, words)))
this would replace 'random'
in 'random words'
but not in 'pseudorandom words'
. 这将取代
'random'
'random words'
'random'
中的'random words'
而不是'pseudorandom words'
中的'pseudorandom words'
。
try this: 试试这个:
prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
themessage = str(word[1])
for word in prohibitedwords:
themessage = themessage.replace(word, "(I'm an idiot)")
print themessage
Code: 码:
prohibitedWords =["MVGame","Kappa","DatSheffy","DansGame",
"BrainSlug","SwiftRage","Kreygasm",
"ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
themessage = 'Brain'
self_criticism = '(I`m an idiot)'
final_message = [i.replace(themessage, self_criticism) for i in prohibitedWords]
print final_message
Result: 结果:
['MVGame', 'Kappa', 'DatSheffy', 'DansGame', '(I`m an idiot)Slug', 'SwiftRage',
'Kreygasm', 'ArsonNoSexy', 'GingerPower', 'Poooound','TooSpicy']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.