I am removing all exact matches in my list_of_strings from a pandas dataframe column. I don't really understand the re.escape that is being used, however. I want to make sure this code will remove ALL matches no matter what type of character is present in my list_of_strings variable and in my dataframe column. What does re.escape really do? I've read the documentation but am newer to regex and would appreciate a more layman's terms explanation.
import pandas as pd
import re
df = pd.DataFrame(
{
"ID": [1, 2],
"name": [
"I have a %$$#form with @#$%$#%@/}\p special characters!!!!",
"can we: remove the EXACT matches !#$#%$^%$&^(*&*)(*&)_&#",
],
}
)
list_of_strings = ['can we: remove', 'with @#$%$#%@/}\p special characters!!!!','EXACT']
p = re.compile('|'.join(map(re.escape, list_of_strings)))
df['cleaned_text'] = [p.sub(' ', text) for text in df['name']]