【发布时间】:2014-11-05 08:54:17
【问题描述】:
我有一个字符串如下:
str1 = "heylisten\uff08there is something\uff09to say \uffa9"
我需要将正则表达式检测到的 unicode 值替换为两边的空格。
所需的输出字符串:
out = "heylisten \uff08 there is something \uff09 to say \uffa9 "
我使用 re.findall 来获取所有匹配项,然后替换它们。它看起来像:
p1 = re.findall(r'\uff[0-9a-e][0-9]', str1, flags = re.U)
out = str1
for item in p1:
print item
print out
out= re.sub(item, r" " + item + r" ", out)
这个输出:
'heylisten\\ uff08 there is something\\ uff09 to say \\ uffa9 '
上面打印了一个额外的“\”并将其与uff分开,这有什么问题?我什至尝试使用re.search,但它似乎只将\uff08 分开。有没有更好的办法?
【问题讨论】:
-
但是您似乎没有更换任何东西? !!
-
我没听懂你。我希望每场比赛的两边都有空格。但 \ 似乎分开了。