Python Regex在string和rep中查找所有匹配项

2024-06-11 05:07:43 发布

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

我在用正则表达式替换字符串时遇到问题,似乎无法使其工作

string = "<font x=''>test</font> <font y=''>test2</font> <font z=''>test3</font>"
if re.search("(<font .*?>)", string, re.IGNORECASE):
    r = re.compile(r"<font (?P<name>.*?)>.*?</font>", re.IGNORECASE)
    string = r.sub(r'', string)

出于某种原因,所有regex都会删除整个字符串''。它应该返回为test test2 test3


Tags: 字符串nametestresearchstringifregex