Regex golf strategies: how to beat par
Seven techniques for writing the shortest regex that catches every target and spares the rest — with live boards you can solve right in the article.

· 5 min read
/^c.*t/
match
- cart
- chart
- coat
spare
- scat
- crab
Regex golf is a simple game: you get two lists of words. Write the shortest regular expression that matches everything on the first list (the monsters) and nothing on the second (the innocents). Every character counts, and each board has a par — the length to beat.
The trick is that regex golf isn't really about regex syntax. It's about seeing. Below are seven habits that separate a 28-character answer from a 5-character one. Every example is live — type into the boards, or hit Try it to open the pattern in the Playground.
01Describe the difference, not the words
The beginner move is to describe the monsters. The golfer's move is to find the smallest thing only the monsters have in common.
Boardcat · cart · chart · coat · cast vs act · arc · tack · scat · crab
- list themsolves it28
/^(cat|cart|chart|coat|cast)$/ - factor outsolves it20
/^c(a|ar|har|oa|as)t$/ - the differencesolves it5
/^c.*t/
All five monsters start with c and have a t somewhere after it. None of the innocents do. That observation is worth 23 characters.
02Anchors are the cheapest filter you have
^ and $ cost one character each and can knock out whole groups of innocents. Watch what happens when we drop the ^ from the answer above:
Two characters of anchor are almost always cheaper than making the rest of the pattern smarter. When a board is split by position — how words start or end — reach for anchors first.
03Search, don't match
Regex golf uses search semantics: your pattern only has to match somewhere in a word, not the whole thing. So there's no need to wrap everything in ^…$ or pad with .*:
| Instead of | Write | Saves |
|---|---|---|
^.*ing$ |
ing$ |
3 |
.*foo.* |
foo |
4 |
^(a|b) |
^[ab] |
1 |
Leading and trailing .* are pure padding. (Regex Hunter's diagnostics panel flags them for exactly this reason.)
04Spend your characters where they count
Many regex features have a short spelling and a long one. Golfers always take the short one:
| Long | Short | Saves |
|---|---|---|
[0-9] |
\d |
3 |
[A-Za-z0-9_] |
\w |
10 |
(a|b|c) |
[abc] |
2 |
.{2} |
.. |
2 |
aa* |
a+ |
1 |
(?:ab)+ |
(ab)+ |
2 |
Non-capturing groups are good hygiene in production code — and wasted characters in golf.
05Let backreferences see patterns for you
When the monsters share a shape rather than specific letters — a doubled letter, a repeated chunk — a backreference does the work. (.) captures any character; \1 demands the same one again.
/(.)\1/Try it on a board. Par is the length of that pattern:
Catch words with a double letter.
Targets · 0/5 caught
- booktarget
- lettertarget
- coffeetarget
- balloontarget
- grasstarget
Innocents · 0 hit
- bakeinnocent
- paperinnocent
- stoneinnocent
- orangeinnocent
- tigerinnocent
Type a regex — the board updates as you go.
06Negative space: say what can't be there
Sometimes the monsters are defined by what they lack. There are two ways to say "no e anywhere", and one is shorter:
Boardmoon · ask · dog · trunk · swift vs tree · eagle · green · apple · code
- lookaheadsolves it8
/^(?!.*e)/ - negated classsolves it7
/^[^e]*$/
Lookaheads earn their keep when you need several conditions at once ("has a digit and no spaces"). For a single missing letter, a negated class wins.
07When nothing's shared, cover the set
Some boards have no single elegant rule. Then the game becomes a puzzle of covering: find a few tiny fragments — j, bu, a.a — that each catch some monsters and no innocents, and join them with |. That's exactly how Peter Norvig's famous solver works — we wrote about it in Regex golf: from xkcd 1313 to a daily game.
08Your turn
One more board. The monsters share a structure, the innocents are chosen to punish the obvious guesses — (.)\1 catches the wrong words, and (.).\1 catches almost everything. Can you hit par?
Catch words that contain a repeated two-letter chunk.
Targets · 0/5 caught
- bananatarget
- papayatarget
- cocoatarget
- dodotarget
- hahatarget
Innocents · 0 hit
- abbainnocent
- llamainnocent
- saladinnocent
- tattooinnocent
- cheeseinnocent
Type a regex — the board updates as you go.
09Keep playing
- Every day there's a new hunt with a par to beat — play today's.
- Practice packs group boards by technique — anchors, classes, quantifiers, backreferences — start with the basics.
- Stuck on syntax? The Regex Guide covers every token, and the Playground lets you test anything.