本页面有中文版。
Cette page est disponible en français.
Esta página está disponible en español.
Regex golf2 live hunts inside

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.

Regex Hunter logo

· 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:

Live huntpar 4

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.

Try it in the Playground
Like this? A new one drops every day.Play today’s hunt

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:

Matches11/11 as expected
/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
Try it in the Playground
xkcd's 10-character answer. Note the spaces — they're part of the pattern.

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.

Pattern63 chars · 13 fragments
/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

xkcd #1313's presidents regex — 13 tiny fragments joined with |.

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:

Matches9/10 as expected
/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
Try it in the Playground
Winners should match, losers shouldn't. One loser gets caught.

[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:

  1. Collect candidate parts. Take every short fragment of the winners' names — one to four characters, with some letters swapped for . — like bu, a.a or r.e$.
  2. Keep only the safe ones. A part is useful only if it matches at least one winner and no losers.
  3. 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.
  4. Join them with |.

The result beat the comic by 9 characters:

Compare14% shorter

Boardlincoln · obama · kennedy · reagan · washington vs fremont · romney · mccain · dewey · gore

  1. 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/
  2. 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/
Try the short one
The comic's regex vs. Norvig's solver, on a sample of winners and losers. Norvig's also passes the full list of every president.

It shaved the Star Wars panel too — from 10 characters to 9:

Compare10% shorter

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

  1. xkcdsolves it10
    /M | [TN]|B/
  2. norvigsolves it9
    / T|E.P|PE/
Try the short one

04From puzzle to daily ritual

A short history of regex golf: xkcd #1313 in January 2014 (63 characters), Norvig's notebook the same month (54), browser level packs, and Regex Hunter's daily hunt

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?

Live huntpar 10

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.

Try it in the Playground
Like this? A new one drops every day.Play today’s hunt

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.