With this code I get:
newContent='asdf asdf \nOUTPUT_DIRECTORY = working\topOnly'
I’d expect it to be:
newContent='asdf asdf \nOUTPUT_DIRECTORY = working\\topOnly'
The reg ex is stripping out a """\""" between """working""" and """topOnly""". If I do something like """valStr = 'working\\ytopOnly'""" then it works as expected.
What do I need to change in my code so that it was as expected?
import re
valStr = 'working\\topOnly'
cmdFileContent = 'asdf asdf \nOUTPUT_DIRECTORY = asdf'
name = 'OUTPUT_DIRECTORY'
varRegEx = r"""
(\n #match new line
%s #var
\s* #skip white spaces
=
\s?) #skip white spaces
(.*) #match and store rest of line
"""%name
newContent = re.compile(varRegEx, re.VERBOSE).sub(r'\1%s'%valStr, cmdFileContent)
The help for re.sub says:
Since it’s doing its own layer of processing escapes, you will need another layer of escapes: