擅长:python、mysql、java
<p>在Python中,您可以使用这个基于交替的regex来匹配所有内部带有单引号的单词并忽略<code>'word1 word2'</code>单词:</p>
<pre><code>s = r"Despite trying and trying I haven't found a regex to capture all these words 'but not capturing these'"
print filter(None, re.findall(r"'[^']*'|(\b\w+(?:'\w+)?\b)", s))
//=> ['Despite', 'trying', 'and', 'trying', 'I', "haven't", 'found', 'a', 'regex', 'to', 'capture', 'all', 'these', 'words']
</code></pre>
<p><a href="https://regex101.com/r/aH1gM8/2" rel="nofollow">RegEx Demo</a></p>