I'm struggling with a following piece of code:
import re
datain = "u_func/u_cpu/uSC300"
elements = ["cpu","123"]
SEARCH = r"([0-9_/])%s([0-9_/])" % elements[0]
REPLACE = r'\1%s\2' % elements[1]
dataout = re.sub(SEARCH, REPLACE, datain, flags=re.IGNORECASE)
print datain
print dataout
The output of the code is following:
u_func/u_cpu/uSC300
u_func/uJ3/uSC300
Please note that cpu
is not substituted by 123
, but with something else.
If I change the LINE 5 to:
REPLACE = r'\1 %s\2' % elements[1]
Then the correct string is inserted:
u_func/u_cpu/uSC300
u_func/u_ 123/uSC300
But there is a space added as well. Of course, I don't want the space.
Question: How to substitute the correct string, without the space being added?