<div>
</div>
<p>我正在遍历列表“titles”中的字符串,我想打印“keywords\u to\u match”中单词匹配的字符串:</p>
<pre class="lang-py prettyprint-override"><code># import re
titles = ['walk to new zealand' , 'fly to mars' , 'drive to murica']
keywords_to_match = re.compile(r'(new zealand)?(mars)?(murica)')
for title in titles:
# if any of words inside keywords_to_match are matched print title
if keywords_to_match.search(title.lower()):
print(title)
# only prints "drive to murica"
</code></pre>
<p>这只打印“drive to murica”,但我希望它打印“titles”中的所有3个字符串</p>