我的目标是使用正则表达式查找单词列表中的所有匹配项。我得到了它的工作,但我希望能够指定多个字母,并且每个字母只出现它指定的次数。
我的代码:
import re
with open('wordlist.txt') as f:
content = f.readlines()
def search(regex):
pattern=re.compile(regex)
for word in content:
word=word.strip()
if(pattern.findall(word)):
print(word)
示例:
search(r'^(b|e|a|c|h|e|s|q|r){7}$') match only words with 7 of those 9 letters. only letters in the word can be those 9. In this case beaches would be returned
search(r'^(f|o|o|c|l|t){4}$') match only words with 4 of those 6 letters. only letters in the word can be those 6. In this case foot and fool and colt would be returned
search(r'^(f|o|d|c|l|t){4}$') match only words with 4 of those 6 letters. only letters in the word can be those 6. In this case only colt would be returned
答案 0 :(得分:1)
我不认为正则表达式是去这里的方式。你不关心订单,只关心每封信的数量。这听起来像是一个数组或dict
的工作。
如何将参数设为search
dict
,其中键是每个字母,值是允许出现该字母的次数?然后只需深度复制dict
,遍历字符串,然后递减。如果找不到密钥,或者密钥已经为0,则会失败并转到下一个字符串。