Solve Wordle puzzles with regular expressions
King of the Wordle
Five letters, one word, six tries – that's Wordle. You can solve any Wordle in just a few steps and gain practical experience using grep and regular expressions.
You've probably come across strange posts on various social media platforms recently where users have shared images of what at first glance appears to be a very simple word game. Typically, you'll see a grid of five by six boxes colored either gray, yellow, or green populated by five-letter words that don't seem to have anything in common.
If you've steered clear of the hype so far, this phenomenon goes by the name of Wordle [1]. Launched in October 2021, the free and currently ad-free Wordle was quickly acquired by The New York Times Company from US software developer Josh Wardle for a "low seven-figure sum" – rumors on the web claim the actual sum was $6 million [2].
To solve Wordle puzzles, you need an extensive vocabulary. If you want to make things a little easier, you can use a dictionary file and some regular expressions and create your own Wordle solver. From an IT point of view, Wordle offers an ideal practical example for getting started with grep
and regular expressions.
Numerous Wordle Clones
All the hubbub about Wordle and the game's simple structure have already prompted numerous developers to program clones. In addition to various web-based imitations, there are also apps for mobile operating systems such as Android and iOS. There are even native Wordle imitators for Linux, such as Warble [3]. Like the original, all of these applications use an English-language dictionary (Figure 1).
The easiest approach to implementing a Wordle game is probably via the command line because the game does not need complex graphics. For Linux, an open source Wordle [4] (Figure 2) can be downloaded with a few simple commands (Listing 1).
Listing 1
Wordle in the Terminal
01 $ wget https://github.com/ivanjermakov/wordle/releases/latest/download/wordle 02 $ chmod +x wordle 03 $ ./wordle
In addition to clones, sites such as The Word Finder [5] or Wordle Solver [6] can help you find a solution (the source code for Wordle Solver is available on GitHub [7]).
Basics of the Game
To solve a Wordle, you don't have to learn complicated rules. You have six attempts at each Wordle. In the first step, just enter an arbitrary five-letter word in the top line. On your first attempt, there are no clues; your first guess is completely random.
After pressing Enter, Wordle checks the input. If a field lights up green, the letter you entered is correct and is in the right place. If the field shows up yellow, the letter is part of the word, but currently in the wrong place. A letter with a dark gray background, on the other hand, is not part of the target word. With the help of these hints, you can narrow down the target word in the following lines until all of the letter fields light up green and you have solved the Wordle. You have a total of six attempts (Figure 3); there is always only one Wordle a day (see the "Second Chance" box).
Second Chance
Wordle usually only lets you play once a day. Instead of implementing this function via an account system, the providers simply set a cookie in the browser that identifies the user. If you are not worried about your statistics, you can simply delete the corresponding cookie and have another go at discovering the word of the day. To manage the cookies stored by the current website (e.g., in Chrome), click on the lock icon to the left of the address bar and select Cookies from the menu that then opens. Then select the cookies for the page and press Remove. Reload the page by pressing F5 to restart the game. However, this still only leaves you with one search term a day.
Using Regexes
You can solve a Wordle using the Linux grep
tool and a couple of simple regular expressions (regexes), which can be used to automatically process and filter texts and strings. If you have not worked with these tools previously, they are indispensable in the everyday life of software developers. Using these tools to solve a Wordle is a wonderful introduction to this topic with huge practical benefits. To solve a Wordle, we will work our way through the hints step by step, and at the end the computer will output the solution.
For this to work, however, you first need a dictionary. A dictionary may not exist on your Linux system, but check out /usr/share/dict/
first – you might find a file that fits the bill. For a word list, you will use words_alpha.txt
, which you can get from GitHub [8]. This lists contains over 370,000 words, including some pretty quirky ones, as you'll see later.
Buy this article as PDF
(incl. VAT)