I have the folowing expression:
a = 'x11 + x111 + x1111 + x1'
and I would like to replace the following:
from_ = ['1', '11', '111', '1111']
to = ['2', '22', '333', '3333']
and therefore obtain the following result:
anew = 'x22 + x333 + x3333 + x2'
How can I do this using Python?
This is a similar question to: Python replace multiple strings. However in my case the replaced values are being overwiten by themselves if I use the suggested anwsers in the question. Hence, in the metioned link the result is 'x22 + x222 + x2222 + x2'
re.sub
from the re
library (regex) can be used whenever you need to do multi-value replacements.
re.sub
takes in the additional argument of a function, in that function you can make the necessary change. From the documentation
re.sub(pattern, repl, string, count=0, flags=0)
If repl
is a function, it is called for every non-overlapping
occurrence of pattern. The function takes a single match object
argument, and returns the
replacement string.
(emphasis mine)
The regex here is simple, i.e, \d+
which implies that you are matching all the groups of digits.
You can utilize the following code snippet to get your desired output
import re
a = 'x11 + x111 + x1111 + x1'
def substitute(matched_obj):
from_ = ['1', '11', '111', '1111']
to = ['2', '22', '333', '3333']
part = matched_obj.group(0)
if part in from_:
return to[from_.index(part)]
return part
anew = re.sub(r'\d+',substitute,a)
After executing the program the value of anew
will be x22 + x333 + x3333 + x2
which is the expected answer.
`