Python:Sub不替换模式替换

时间:2013-05-28 09:17:43

标签: python replace

我一直在使用这段代码,似乎是一辈子的事情,似乎无法让它发挥作用。

translation = re.sub("'''[a-zA-Z0-9].*?'''", "<b>[what do I put here to copy [a-zA-Z0-9].*?</b>", translation)

Here I'm trying to replace " ''' [text] ''' " with " <b> [text] </b>. What would I have to put between <b> and </b> to make it copy across the text?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

使用捕获组并按\1

引用它
translation = re.sub(r"'''([a-zA-Z0-9].*?)'''", r"<b>\1</b>", translation)

答案 1 :(得分:0)

translation = re.sub("'''([a-zA-Z0-9]*)'''", r"<b>\1</b>", translation)

那样的东西?