有了這個代碼,我得到:蟒蛇正則表達式剔除了反斜線
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