xkcd regex golf: the puzzle behind comic 1313
How xkcd's regex golf comic started a puzzle genre, how Peter Norvig solved it, and where to play a new regex golf puzzle every day.

· 5 min read · Updated
/j|bu|oo/
match
- jefferson
- buchanan
- hoover
spare
- fremont
- dewey
- gore
Regex golf, the puzzle from xkcd comic 1313, is the game of writing the shortest regular expression that matches every word in one list and none in another. Like real golf, fewer strokes win. Here's the whole idea in one board — try it:
Catch the words that end in -ing, spare the rest.
Targets · 0/5 caught
- singtarget
- ringtarget
- goingtarget
- thingtarget
- swingtarget
Innocents · 0 hit
- ingotinnocent
- fringeinnocent
- kingsinnocent
- dingoinnocent
- hingeinnocent
Type a regex — the board updates as you go.
That's the entire game. The fun is in how quickly it stops being easy.
01The comic that named it
In January 2014, xkcd #1313, "Regex Golf" introduced a lot of programmers to the game. Its first panel shows a regex that matches Star Wars subtitles but not Star Trek ones:
/M | [TN]|B/Should match · Star Wars6/6
- THE PHANTOM MENACE
- ATTACK OF THE CLONES
- REVENGE OF THE SITH
- A NEW HOPE
- THE EMPIRE STRIKES BACK
- RETURN OF THE JEDI
Should not · Star Trek5/5
- THE WRATH OF KHAN
- THE SEARCH FOR SPOCK
- THE VOYAGE HOME
- THE FINAL FRONTIER
- THE UNDISCOVERED COUNTRY
And the comic's hover text raises the stakes: a 63-character pattern that matches the last names of every elected US president but none of the candidates they beat.
/bu|[rn]t|[coy]e|[mtg]a|j|iso|n[hl]|[ae]d|lev|sh|[lnd]i|[po]o|ls/Tap a fragment to find it
It looks like line noise, but read it as a list: each fragment between the | bars catches a few presidents. j alone catches Jefferson, Jackson and Johnson.
02The bug in the punchline
That same month, Peter Norvig — then Director of Research at Google, and co-author of the standard AI textbook — wrote a notebook to solve regex golf automatically. The first thing his test did was check the comic's answer. It failed:
/bu|[rn]t|[coy]e|[mtg]a|j|iso|n[hl]|[ae]d|lev|sh|[lnd]i|[po]o|ls/Should match · Winners5/5
- lincoln
- obama
- kennedy
- reagan
- washington
Should not · Losers4/5
- romney
- mccain
- dewey
- gore
- fremont
[rn]t catches fremont — John C. Frémont, who lost the 1856 election to James Buchanan. Norvig worked out that Randall had used Millard Fillmore, the third-party candidate that year, as Buchanan's opponent instead. A regex-golf answer is only as good as its word lists.
03How a computer plays regex golf
Norvig's solver is a lovely piece of reasoning, and it's worth knowing because it's how good human golfers think, too:
- Collect candidate parts. Take every short fragment of the winners' names — one to four characters, with some letters swapped for
.— likebu,a.aorr.e$. - Keep only the safe ones. A part is useful only if it matches at least one winner and no losers.
- Cover the winners. Now pick parts until every winner is matched. That's the classic set cover problem, which is NP-hard — so the solver picks greedily, each time taking the part with the best trade-off between winners covered and characters spent.
- Join them with
|.
The result beat the comic by 9 characters:
Boardlincoln · obama · kennedy · reagan · washington vs fremont · romney · mccain · dewey · gore
- xkcd #13135/5 caught, 1 wrong63
/bu|[rn]t|[coy]e|[mtg]a|j|iso|n[hl]|[ae]d|lev|sh|[lnd]i|[po]o|ls/ - norvig's solversolves it54
/a.a|a..i|j|li|a.t|i..n|oo|bu|ay.|n.e|r.e$|ru|ls|po|v.l/
It shaved the Star Wars panel too — from 10 characters to 9:
BoardTHE PHANTOM MENACE · ATTACK OF THE CLONES · REVENGE OF THE SITH · A NEW HOPE · THE EMPIRE STRIKES BACK · RETURN OF THE JEDI vs THE WRATH OF KHAN · THE SEARCH FOR SPOCK · THE VOYAGE HOME · THE FINAL FRONTIER · THE UNDISCOVERED COUNTRY
- xkcdsolves it10
/M | [TN]|B/ - norvigsolves it9
/ T|E.P|PE/
04From puzzle to daily ritual
Browser games like regex.alf.nu turned the idea into level packs you could grind through. Regex Hunter takes the other road — the Wordle road:
One hunt a day
The same for everyone, with a par to beat and a streak to keep.
Practice packs
Each drills one technique, from anchors to backreferences.
Community hunts & sprees
Hunts written by players, and live races against other people on the same boards.
Academy & Playground
Learn regex from scratch, then test anything you like.
05One from Norvig's notebook
Norvig also pitted the most popular baby names against each other. Here are five boys and five girls — par is his answer's length. Can you tie it, or beat it?
Catch the boys' names, spare the girls' names.
Targets · 0/5 caught
- jacobtarget
- masontarget
- ethantarget
- noahtarget
- aidentarget
Innocents · 0 hit
- madisoninnocent
- emilyinnocent
- abigailinnocent
- sophiainnocent
- oliviainnocent
Type a regex — the board updates as you go.
06Play regex golf today
Play today's hunt → A new regex golf puzzle every day: catch the monsters, spare the innocents, beat par. Free, in your browser, no sign-up.
If you want technique rather than history, read Regex golf strategies: how to beat par.