'Using Regular Expression to narrow down dictionary based on Wordle rules
I am trying to do Wordle Solver as my resume project. I want to develop some word suggestions by narrowing down dictionary of words using RegEx.
Is it possible to write RegEx such that it searches for words in the dictionary that satisfy these 3 conditions?
- Word that starts with the letter 'C'
- does not contain letter 'T' anywhere in the word
- The word overall must contain letter 'E' somewhere but not a first(word starts with 'C') and third positions?
My attempt is below but I'm failing with the 3rd requirement.
[c][^\Wt][^\Wte][^\Wt][^\Wt]
Solution 1:[1]
The below assumes you use flags to enable case insensitivity and multiline mode (so ^ matches the beginning of a line and $ the end) - re.I and re.M.
Word that starts with the letter 'C'
This is just ^C.*$
Does not contain letter 'T' anywhere in the word
This can be accomplished with the positive lookahead (?=^[^T]*$)
The word overall must contain letter 'E' somewhere but not a first and third positions
This is a bit tricker, but doable:
- assure the text contains an
Esomewhere(?=.*E) - assure an
Eis not in the third position(?!^..E)
Gluing it all together (and pulling the ^'s out front):
^(?=[^T]*$)(?=.*E)(?!..E)C.*$
Solution 2:[2]
This is just an example. You could do some little helper functions to make it more flexible. functions like "letter isn't in the word at all", "specific letter at 3rd position" ....
dic = {'words': ['value', 'cache', 'cacao', 'racer', 'house']}
filt = [x for x in dic['words'] if all([x[0].upper()=='C', 'T' not in x.upper(), 'E' in x.upper()])]
print(filt)
['cache']
List comprehension with if condition. all conditions are wrapped in all() which only returns True if every single statements is True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dillon Davis |
| Solution 2 |
