擅长:python、mysql、java
<p>除非指定<code>re.DOTALL</code>标志,否则点与换行符不匹配。在</p>
<pre><code>re.sub("</([a-zA-Z]+)>.*?<","</\\1><",text, flags=re.DOTALL)
</code></pre>
<p>应该能正常工作。(如果不是,我的python错了,而不是regex。请更正。)</p>
<p>我认为在定义要重复的字符类时,尽可能精确是一个很好的实践。这有助于防止<a href="http://www.regular-expressions.info/catastrophic.html" rel="nofollow">catastrophic backtracking</a>。因此,我将使用<code>[^<]*</code>而不是<code>.*?</code>,它现在可以在最后一个标记之后找到离群字符。这将不再需要<code>re.DOTALL</code>标志,因为<code>[^<]</code>确实与换行符匹配。在</p>