Skip to content Skip to sidebar Skip to footer

Remove List Of Phrases From String

I have an array of phrases: bannedWords = ['hi', 'hi you', 'hello', 'and you'] I want to take a sentence like 'hi, how are tim and you doing' and get this: ', how are tim doing'

Solution 1:

Since you want to remove extra spaces as well, the regex below should work better:

s = "Hi, How are Tim and you doing"
bannedWords = ['hi', 'hi you', 'hello', 'and you']
for i in bannedWords: 
    s = re.sub(i + "\s*", '', s, flags = re.I)
print s
# ', How are Tim doing'

Solution 2:

You can use re.sub with a flag to do this in a case insensitive manner.

import re

bannedWords = ['hi', 'hi you', 'hello', 'and you']
sentence = "Hi, how are Tim and you doing"

new_sentence = re.sub('|'.join(bannedWords) + r'\s+', '', sentence, flags=re.I)
# new_sentence: ", how are Tim doing"

Solution 3:

With regex you can join words you want to remove with |. We also want to remove any multiple blankspace with one blankspace. This ensures we only do two operations.

import re

def remove_banned(s,words):
    pattern = '|'.join(words)
    s = re.sub(pattern, '', s, flags = re.I) # remove words
    s = re.sub('\s+', ' ', s, flags = re.I) # remove extra blank space'
    return s

bannedWords = ['hi', 'hi you', 'hello', 'and you']
s = "Hi, How are Tim and you doing"

print(remove_banned(s,bannedWords))

Returns:

, How are Tim doing

Post a Comment for "Remove List Of Phrases From String"