选择仅包含特定字母的单词

时间:2015-06-02 17:23:09

标签: regex python-2.7

我正在尝试仅打印与我提供的正则表达式匹配的单词,并且只包含第二个参数中提供的字母。正则表达式完美无缺,但字母选择没有。

import re

def search(regex,letters):
    letters=set(letters)
    pattern=re.compile(regex)
    for word in content:
        word=word.strip()
        if(pattern.findall(word)):
            if letters & set(word):
                print(word)


#search(r'^(n|u|p|g|o|u|e|b|l){6}$')
#search(r'^t(i|a)[a-z]{3}')
content=["hello","helps","halts","random"]
search(r'^h(e|a)[a-z]{3}','hltsa') #returns: hello,helps,halts

我的目标是让它只返回暂停,因为它匹配第二个参数。

1 个答案:

答案 0 :(得分:0)

Form::open(array('action' => 'Controller@method'))

试试这个,我只更改了import re def search(regex,letters): letters=set(letters) pattern=re.compile(regex) for word in content: word=word.strip() if(pattern.findall(word)): if set(word) >= letters: print(word) #search(r'^(n|u|p|g|o|u|e|b|l){6}$') #search(r'^t(i|a)[a-z]{3}') content=["hello","helps","halts","random"] search(r'^h(e|a)[a-z]{3}','hltsa') #returns: hello,helps,halts

在集合的文档中,您可以找到此

if letters & set(word):