Skip to content
Advertisement

Replace a list of elements with regex

I have a text full of adverbes and it’s replacements like this :

JavaScript

And i want the adverbes to replaced in my text:

Example :

JavaScript

but am runing out of solutions, my code so far:

JavaScript

I couldn’t find a way to replace each adverbes with the appropriate replacement.

the list_adverbes_replacement.txt is the text i gave in the beginning, and please am looking for a regex solution, i just don’t know what am missing.

Advertisement

Answer

Simple and concise approach. Build a dictionary of key/value pairs for your replacements.

Then replace them using regex’ re.sub by matching on each word, looking up the word in the dictionary, and defaulting to the word itself if it’s not in the dictionary

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement