搜索多个字符串

2024-04-19 05:02:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下Python代码:

string = '[subscript=hello] this is some text [subscript=hi again]'
superscripts = re.findall(r'\[subscript=(.+?)\]', string)
print superscripts

它返回['hello', 'hi again'],这是我想要的文本,但是,我宁愿它替换字符串,以便它返回<sub>hello</sub> this is some text <sub>hi again<sub>,而不是返回这个。我知道我应该用回复sub()但我不确定如何使用它来正确地替换字符串。你知道吗

我该怎么做?谢谢。你知道吗

编辑:屏幕截图

inputresult


Tags: 字符串代码textrehellostringissome