<p>我正在尝试在以下字符串中进行替换:</p>
<pre><code>poem='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
'''
</code></pre>
<p>要求如下:</p>
<ol>
<li>如果模式有字符'ai'或'hi',用<code>*\*</code>替换接下来的三个字符。在</li>
<li>如果一个单词有“ch”或“co”,则将其替换为“ch”或“co”。在</li>
</ol>
<p>我尝试了以下方法:</p>
^{pr2}$
<p><strong>输出:</strong></p>
<pre><code>If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one f(ai|hi)*\*ng robin
Unto his nest again,
I shall not live in vain.
print(re.sub(r"ch|co",r"Ch|Co",poem))
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aCh|Coing,
Or Ch|Cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
</code></pre>
<p>你可以看到输出不符合要求。请帮助我找到正确的正则表达式。在</p>