<p>假设a有一个单词列表,并想检查每个单词是否与模式“a”匹配?在哪里?可以是任何字母数字字符。另外,我想忽略包含“-”(破折号)的单词。如何使用regex执行此操作?你知道吗</p>
<p>以下是我目前的情况:</p>
<pre><code>for word in words:
found = re.findall(r'([a]\w{1}[e])(^((?!(-)).)*$)', word)
if found:
print(word)
</code></pre>
<p>例如:['snake'、'take'、'want'、'sane'、'1'、'snakke'、's-ake']</p>
<p>我想过滤掉:<code>snake, take, sane</code></p>
<p>编辑:抱歉,我的问题/代码不匹配</p>
<p>使用<code>match</code>而不是<code>findall</code>。<code>match</code>将匹配整个字符串,而<code>findall</code>将查找与正则表达式匹配的子字符串。你知道吗</p>
<pre><code>found = (w for w in words if re.match(r'g\wo', word))
</code></pre>