Regex for Wordle

Viewed 2118

using the online word game Wordle (https://www.powerlanguage.co.uk/wordle/) to sharpen my Regex.

I could use a little help with something that I imagine Regex should solve easily.

  • given a 5 letter english word
  • given that I know the word begins with pr
  • given that I know that the letters outyase are not found in the word
  • given that I know that the letter i IS found in the word

what is the correct - most simplified regex?

my limited regex gives is this ^pr.[^outyase][^outyase]$ which is

  • a. redundant and
  • b. does not include the request to match i

any of you Regex Ninjas want to lend a hand, I would be much obliged.

by the way, the correct regex should return two nouns in the english language prick and primi, you can validate here https://www.visca.com/regexdict/

4 Answers

You may use this regex with a positive and negative lookahead conditions:

^pr(?=[a-z]*i)(?![a-z]*[outyase])[a-z]{3}$

Regex Explanation:

  • ^: Start
  • pr: Match pr
  • (?=[a-z]*i): Positive lookahead to make sure we have an i ahead after 0 or more letters
  • (?![a-z]*[outyase])): Negative lookahead to disallow any of the [outyase] characters
  • [a-z]{3}: Match 3 letters

Demo Screenshot:

enter image description here

Trivially, you can use:

^pr([^outyase][^outyase]i|[^outyase]i[^outyase]|i[^outyase][^outyase])$

Also, according to your site, there's actually four words matching, not just two:

  • prick
  • primi
  • primp
  • prink

Try

^pr(?!.*[outyase])(?=.*i)[a-z]{3}$

(?!.*[outyase]) means don't match if any of outyase is found ahead in the string.

(?=.*i) means only match if there is an i ahead in the string.

Adding a note for general usage.

For any char position:

(?!.*[<BadChars>])(?=.*<firstGoodChar>)(?=.*<SecondGoodChar>)(?=.*<ThirdGoodChar>).*

If you know mghtlc are bad and i, o, and s are good:

^(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{5}$

It's trivial to add a pinned char at the front/back:

^b(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{4}$
^(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{4}n$

but I'm not sure how the look-ahead would work with a pinned char in the middle given that the found chars (i, o) can be on either side of the pinned s:

NOT WORKING:

^(?!.*[mghtlc])(?=.*i)(?=.*o).*{2}s(?!.*[mghtlc])(?=.*i)(?=.*o).*{2}$
Related