擅长:python、mysql、java
<p>您应该转义<code>\</code>字符或使用<code>r''</code>原始字符串:</p>
<pre><code>>>> re.sub('handle(.*?)', r'<verse osisID="lol">\1</verse>', 'handle a bunch of random text here.')
'<verse osisID="lol"></verse> a bunch of random text here.'
</code></pre>
<p>如果没有<code>r''</code>原始字符串文本,反斜杠将被解释为转义代码。也可以将反斜杠加倍:</p>
^{pr2}$
<p>注意,只替换单词<code>handle</code>,其中<code>.*?</code>模式至少匹配0个字符。去掉问号,它将符合您的预期输出:</p>
<pre><code>>>> re.sub('handle(.*)', r'<verse osisID="lol">\1</verse>', 'handle a bunch of random text here.')
'<verse osisID="lol"> a bunch of random text here.</verse>'
</code></pre>