Issue
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
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
E
somewhere(?=.*E)
- assure an
E
is not in the third position(?!^..E)
Gluing it all together (and pulling the ^
's out front):
^(?=[^T]*$)(?=.*E)(?!..E)C.*$
Answered By - Dillon Davis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.