Replace a list of elements with regex

Tags: ,



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

adverbe1 |replacement1
adverbe2 |replacement2
adverbe3 |replacement3

And i want the adverbes to replaced in my text:

Example :

'Hello adverbe1 this is a test' to be this : 'Hello replacement1 this is a test'

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

adverbes = open("list_adverbes_replacement.txt", encoding="utf-8")
list_adverbes = []
list_replacement = []
for ad in adverbes.readlines():
    if ad != '' and ad.split('|')[0].strip(' ')[-3:] == 'ent':
        list_adverbes.append(ad.split('|')[0].strip(' '))
        list_replacement.append(ad.split('|')[1])
pattern = r"(s+b(?:{}))b".format("|".join(list_adverbes))
data = re.sub(pattern, r"1", data)

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.

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

import re

d = dict()
with open('list_adverbes_replacement.txt', 'r') as fo:
    for line in fo:
        splt = line.split('|')
        d[splt[0].strip()] = splt[1].strip()

s = 'Hello adverbe1 this is a test, adverbe2'
s = re.sub(r'(w+)', lambda m: d.get(m.group(), m.group()), s)
print(s)


Source: stackoverflow