<p>我习惯用<code>re.sub()</code>来替换字符串中的字母,但是仅仅插入一些东西怎么样?你知道吗</p>
<pre><code>import re
re.sub('Item', '- <thing>', 'A list of things: \nItem 1 \nItem 2')
</code></pre>
<p>你应该回来</p>
<pre><code>"A list of things: \n- Item 1 \n- Item 2"
</code></pre>
<p>它并没有真正替代,而是插入了一些东西。这在正则表达式中真的可能吗?还是我应该坚持在整个文本中循环使用<code>.replace()</code>?我需要替换特定事物的模式,因此使用大量<code>.replace()</code>似乎有点不雅观。你知道吗</p>
<p>为什么不做第二个参数<code>- Item</code>?你知道吗</p>
<pre><code>>>> re.sub('Item', '- Item', 'A list of things: \nItem 1 \nItem 2')
'A list of things: \n- Item 1 \n- Item 2'
</code></pre>