Home > Software design >  Using Regular Expression to narrow down dictionary based on Wordle rules
Using Regular Expression to narrow down dictionary based on Wordle rules

Time:05-15

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?

  1. Word that starts with the letter 'C'
  2. does not contain letter 'T' anywhere in the word
  3. 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]

CodePudding user response:

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

CodePudding user response:

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.*$
  • Related