擅长:python、mysql、java
<p>您可以使用以下代码:</p>
<pre><code>import re
string = "Foo \n value of something \n Bar \n Another value \n"
changewords = ["foo", "Bar"]
tem = string
for i in changewords:
tem = re.sub(f'(?i){i} \n', f'{i}:', tem)
print( tem )
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>foo: value of something
Bar: Another value
</code></pre>
<p>注意<code>tem = string</code>初始化<code>tem</code>值,然后在<code>for</code>循环中使用<code>re.sub</code>on <code>tem</code>并将返回结果赋给<code>tem</code>本身</p>
<p><code>(?i)</code>用于忽略大小写匹配</p>
<p><a href="https://ideone.com/AVfOHR" rel="nofollow noreferrer">Code Demo</a></p>