Reputation: 9763
My goal is to find all matches in a word list using regex. I got it working, but I would like to be able to specify multiple letters and have each letter occur only the number of times it is specified.
my code:
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)
Examples:
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
Upvotes: 1
Views: 34
Reputation: 790
I don't think that a regex is the way to go here. You don't care about order, just about how many of each letter there are. That sounds like a job for an array or a dict
.
How about making the argument to search
a dict
where the keys are each letter, and the values are the number of times that letter is allowed to appear? Then just deep copy the dict
, iterate over the string, and decrement. If the key's not found, or it's already 0, fail and move on to the next string.
Upvotes: 1