I have the following string:
s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
I want to replace all words that match words in the month_set list with the word month. The output should look like
'in month 1999, nothing really happened, same as in month 1999'
The
for month in month_set:
sentence = s.replace(month, 'date')
print(sentence)
but that returned the following:
in may 1999, nothing really happened, same as in june 1999 in date 1999, nothing really happened, same as in june 1999 in may 1999, nothing really happened, same as in date 1999
Beside, if the above works, I need to apply it to a big list of strings which will make it slow, I think.
You can try this:
s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = ' '.join("month" if i in month_set else i for i in s.split())
Output:
'in month 1999, nothing really happened, same as in month 1999'
A pure regex solution:
import re
s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = re.sub('|'.join("(?<=\s){}(?=\s)".format(i) for i in month_set), 'month', s)
print(final_string)
s1 = 'may june mayor'
final_string1 = re.sub('|'.join("((?<=\s)|(?<=^)){}((?=\s)|(?=$))".format(i) for i in month_set), 'month', s1)
print(final_string1)
Output:
'in month 1999, nothing really happened, same as in month 1999'
'month month mayor'
Use regex:
import re
months = ['May', 'June']
myRegex = re.compile("|".join(months))
myRegex.sub("Month", "Welcome to May, next is June")
This version is case-sensitive but if you want case-insensitive version just use:
myRegex = re.compile("|".join(months), re.IGNORECASE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With