有了这个代码,我得到:蟒蛇正则表达式剔除了反斜线
newContent='asdf asdf \nOUTPUT_DIRECTORY = working\topOnly'
我希望它是:
newContent='asdf asdf \nOUTPUT_DIRECTORY = working\\topOnly'
的REG EX是"""working"""
和"""topOnly"""
间剥离出一"""\"""
。如果我执行类似"""valStr = 'working\\ytopOnly'"""
的操作,则按预期工作。
我需要在我的代码中更改哪些内容才能达到预期效果?
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)
真实和预期的输出是相同的问题,请编辑它。 – utdemir 2011-05-17 18:24:40