擅长:python、mysql、java
<p>通过对每个字符串分别应用基于regex的搜索,可以大大简化您的解决方案</p>
<pre><code>>>> p = re.compile('d.*e')
>>> list(filter(p.search, l))
</code></pre>
<p>或者</p>
<pre><code>>>> [x for x in l if p.search(x)]
</code></pre>
<p/>
<pre><code>['chdsye847', 'hedle3455', 'xjhd53e', 'de37dp']
</code></pre>
<hr/>
<p>为什么<code>re.findall</code>不起作用?你在搜索一个大的字符串,而你中间贪婪的对手在搜索整个字符串。解决办法是</p>
<pre><code>>>> re.findall(r"\b\S*d\S*e\S*", ' '.join(l))
['chdsye847', 'hedle3455', 'xjhd53e', 'de37dp']
</code></pre>
<p>使用<code>\S</code>匹配任何不是空格的内容</p>