Replace all words from a word list with another line in python

I have a string entered by the user, and I want to search for it and replace any occurrences of the word list 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 works for loops.

+6
source share
3 answers

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' .

+11
source

try the following:

 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 
+4
source

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'] 
0
source

All Articles