擅长:python、mysql、java
<p>好吧,让我们看看您的regex <code>([\n]+)([A-Z])+</code>-第一部分<code>([\n]+)</code>很好,它将一个新行的多个出现匹配到一个组中(注意,这不会匹配回车符<code>\r</code>)。然而,第二部分<code>([A-Z])+</code>指出了您的错误,它将一个大写字母匹配到一个捕获组中—如果有多个大写字母,它将多次将该组重置为最后一个匹配的大写字母,然后将其用于替换。在</p>
<p>试试下面的方法看看会发生什么</p>
<pre><code>import re
i = "Inc\n\nABRAXAS"
i = re.sub(r'([\n]+)([A-Z])+', r"____\2", i)
</code></pre>
<p>您可以简单地将+放在捕获组中,这样就可以将多个大写字母匹配到其中。你也可以省去它,因为它没有什么区别,有多少大写字母跟在后面。在</p>
^{pr2}$
<p>如果您想替换任何一个换行符序列,不管下面是什么-完全删除<code>([A-Z])</code>并尝试</p>
<pre><code>import re
i = "Inc\n\nABRAXAS"
i = re.sub(r'(\n+)', r"____", i)
</code></pre>
<p>如果要考虑回车符,也可以使用<code>([\r\n]+)</code>作为模式</p>