<p>你的误解在于你用来匹配和替换的模式<code>r'\<.+\>'</code>是<a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454"><em>greedy</em>, meaning it will match as much as it possibly can</a>。在您提供的这个示例中,您的模式将获取它找到的第一个<code><</code>和最后一个<code>></code>之间的所有(<code>.+</code>)。您可以在<a href="https://regex101.com/r/RpxY47/2" rel="nofollow noreferrer">Regex101</a>之类的工具中可视化该行为,使其更易于理解</p>
<p>相反,通过将<code>?</code>限定符添加到<code>.+</code>模式中,使您的模式“懒惰”:</p>
<pre><code>import re
s = '彼女は<font color="#ff0000"><font color="#ff0000">看護婦</font></font>です。'
x = re.sub(r'\<.+?\>','',s)
print(x) # 彼女は看護婦です。
</code></pre>
<p><sup><a href="https://repl.it/@esqew/HarmoniousWorrisomeHypertalk#main.py" rel="nofollow noreferrer">Repl.it</a>{a4}</sup></p>
<p>但是,您确实应该为这种类型的活动使用适当的HTML解析器<a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">Regex is generally regarded as not being a good tool for working with HTML content.</a>请参见<a href="https://stackoverflow.com/a/63289452/269970">Juan C's answer to this question</a>了解如何实现这一目标的示例</p>