【发布时间】:2017-02-28 02:51:31
【问题描述】:
我有一个 python 模式,我想用两种不同的方式替换它。
Pattern: <br><b>[Ss][1-2]:[0-9]*-
1)replace by " "
2)replace s/S by Author
我可以做第一个,因为它是一个简单的替换,但我不确定如何做第二个替换,因为它取决于输入模式并且只需要替换表达式的一部分。
这适用于案例 1,我只是用空格替换。
text="<br><b>S1:2- you are wrong. I don't think so. <br><b>S2:2- you are wrong"
newtext=re.sub("(<br><b>[Ss][1-2]:[0-9]*-)\s*", ' ',text)
print(newtext)
我们可以在替换字符串中有一个变量表达式吗?
替换文字
<br><b>Author1:2- you are wrong. I don't think so. <br><b>Author2:2- you are wrong
【问题讨论】:
-
您能否提供第二个用例的替换示例