您不仅需要匹配单词字符,还需要匹配单词边界:
keyword = re.compile(r'\b\w{7}\b')
锚点匹配单词的\b
开头或结尾,将单词限制为正好7 个字符。
如果您逐行遍历文件而不是一次性将其全部读入内存,效率会更高:
import re
keyword = re.compile(r'\b\w{7}\b')
with open("dictionary.txt","r") as dictionary:
for line in dictionary:
for result in keyword.findall(line):
print(result)
Usingkeyword.findall()
为我们提供了在线上所有匹配项的列表。
要检查匹配项中是否至少包含一个必需的字符,我个人只会使用集合交集测试:
import re
keyword = re.compile(r'\b\w{7}\b')
required = set('abcer')
with open("dictionary.txt","r") as dictionary:
for line in dictionary:
results = [required.intersection(word) for word in keyword.findall(line)]
for result in results
print(result)