空白和元我有一個這樣的字符串:替換在Python
str1 = "/path/happy (dog)"
爲了某種目的,我想它想:
str2 = "/path/happy\ \(dog\)"
我試圖重新使用:
str1 = "happy (dog)"
tuple_1 = ('\s+','(',')')
for i in tuple_1:
match = re.search(r"("+i+")",str1)
if match:
str1 = re.sub(match.group(),"\\"+match.group(),str1)
print str1
但它給出了錯誤:
sre_constants.error: unbalanced parenthesis
我知道我在這裏缺少的東西...
你必須在正則表達式中繞過括號。 – 2013-03-13 14:06:13
你是否使用了你的錯誤信息? http://stackoverflow.com/questions/10318248/unbalanced-parenthesis-python – 2013-03-13 14:08:48