<div>
</div>
<p>所以我想找到字符串中所有不在正则表达式中的部分</p>
<p>假设我有一个regex<code>r'foo|bar'</code>和一个字符串<code>'Hello foo how are you bar'</code>,除了regex匹配的单词之外,我怎么能得到每个单词,让它返回<code>['Hello', 'how', 'are', 'you']</code></p>
<p>与@dawg的答案非常相似。但在正则表达式中可以有消极的展望</p>
<pre><code>st='Hello foo how are you bar'
[w for w in st.split() if re.search(r'^(?!(foo|bar))', w)] # ['Hello', 'how', 'are', 'you']
</code></pre>