What I am trying to achieve is to substitute a string using python regex with a variable (contents of the variable). Since I need to retain some of the matched expression, I use the \1 and \3 group match args.
My regex/sub looks like this:
pattern = "\1" + id + "\3" \b
out = re.sub(r'(;11=)(\w+)(;)',r'%s' % pattern, line)
What appears to be happening is \1 and \3 do not get added to the output.
I have also tried this with the substitution expression:
r'\1%s\3'%orderid
But I got similar results. Any suggestion on what might fix this?
\1and\3? They are always the same values, just put them as strings.re.sub(r'(;11=)(\w+)(;)', ";11=" + id + ";", line), or remove the captures completely:re.sub(r';11=\w+;', ";11=" + id + ";", line)(and you don't seem to be using the\w+anyway).