How to extract all of original compounds if a substring is in them with re module?

Viewed 93
string= "'Patriots', 'corona2020','COVID-19','coronavirus','2020TRUmp','Support2020Trump','whitehouse','Trump2020','QAnon','QAnon2020',TrumpQanon"

badwords = ['qanon', 'trump', 'corona', 'COVID']

If a compound in the string contains the sub-string of badwords, then that compound must be deleted from the string. For instance, we have COVID in the badwords, then COVID-19 should be removed in the string.

I tried to use re module like this, but failed:

import re

badwords = ['qanon', 'trump', 'corona', 'COVID']
string = "'Patriots', 'corona2020','COVID-19','coronavirus','2020TRUmp','Support2020Trump',Trump2020,'QAnon'"
for each in badwords:
    print(re.findall ('[0-9a-zA-Z]+'+each,string,flags=re.IGNORECASE)+\
          re.findall (each+'[0-9a-zA-Z]+',string,flags=re.IGNORECASE))

what I want: a new string "'Patriots','whitehouse'" should return.

2 Answers

I am converting every word to upper(or can be lower) so can match every similar word without small cap or capital difference with find

badwords=['qanon','trump','coronavirus','COVID']
string= "'Patriots', 'corona2020','COVID-19','coronavirus','2020TRUmp','Support2020Trump','whitehouse','Trump2020','QAnon','QAnon2020',TrumpQanon"
new_string = string.split(',')

for bad_word in badwords:
 new_string = [every_word for every_word in new_string if every_word.upper().find(bad_word.upper()) == -1]
     
 
string_without_bad_word = ','.join(new_string) #"'Patriots', 'corona2020','whitehouse'"

First, create a regex that would match any of the words in the badwords list:

import re
rex_string = "(" + "|".join(badwords) + ")" # (qanon|trump|corona|COVID)

rex = re.compile(rex_string, re.IGNORECASE)

Then, split() your string by commas to get a list that each element contains one compound word. Next, iterate over this list and if the regex doesn't match the string, add it to your new list of words. Finally, we can join the new list of words into a single string using str.join()

words_list = string.split(",")
new_list = []

for word in words_list:
    if rex.search(word) is None:
        # Didn't find a match
        new_list.append(word)

new_string = ",".join(new_list)

which gives us the string:

"'Patriots','whitehouse'"

If you were so inclined, you could write the loop as a one liner:

new_list = [word for word in string.split(",") if rex.search(word) is None]

Or,

new_string = ",".join(word for word in string.split(",") if rex.search(word) is None)
Related