擅长:python、mysql、java
<p>关键是正则表达式在字符串中的每个字符之前匹配,因为它是可选的,<code>(?:...)?</code>,用<a href="https://www.regular-expressions.info/optional.html" rel="nofollow noreferrer">^{<cd2>} quantifier</a>修改的<a href="https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions">non-capturing group</a>,使它匹配1到0次。你知道吗</p>
<p>而且,<code>\d</code>只匹配一个数字,您还需要考虑字母。你知道吗</p>
<p>为了快速解决问题,您可以使用</p>
<pre><code>st = re.sub(r"\]\s*\w+\.\s*", "] ", st)
</code></pre>
<p>见<a href="http://htttp://regex101.com/r/Lp9vBQ/1" rel="nofollow noreferrer">this regex demo</a>。<code>\w+</code>结构匹配1+个单词字符(字母、数字或下划线)。你知道吗</p>
<p>如果只匹配<code>]</code>之后<code>.</code>之前的1+个数字或1+个字母,可能会使它更精确:</p>
<pre><code>st = re.sub(r"\]\s*(?:\d+|[a-zA-Z]+)\.\s*", "] ", st)
^^^^^^^^^^^^^^^^^
</code></pre>
<p>见<a href="https://regex101.com/r/wUZS8b/1" rel="nofollow noreferrer">another regex demo</a>。你知道吗</p>