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

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.

Regex Hunter logo

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

Compare82% shorter

Boardcat · cart · chart · coat · cast vs act · arc · tack · scat · crab

  1. list themsolves it28
    /^(cat|cart|chart|coat|cast)$/
  2. factor outsolves it20
    /^c(a|ar|har|oa|as)t$/
  3. the differencesolves it5
    /^c.*t/
Try the short one
Same board, three answers. The winner doesn't describe the words at all — it just separates them.

All five monsters start with c and have a t somewhere after it. None of the innocents do. That observation is worth 23 characters.

The trimming ladder: listing the words takes 28 characters, factoring takes 20, describing the difference takes 5

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:

Matches4/6 as expected
/c.*t/

Should match3/3

  • cat
  • chart
  • coast

Should not1/3

  • act
  • scat
  • crab
Try it in the Playground
Without the ^ anchor, "act" and "scat" sneak in — both contain a c followed later by a t.

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.

Pattern5 chars
/(.)\1/
Try it in the Playground
"Any character, then that same character again" — a doubled letter anywhere in the word.

Try it on a board. Par is the length of that pattern:

Live huntpar 5

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.

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

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:

Compare13% shorter

Boardmoon · ask · dog · trunk · swift vs tree · eagle · green · apple · code

  1. lookaheadsolves it8
    /^(?!.*e)/
  2. negated classsolves it7
    /^[^e]*$/
Try the short one
Both work. The negated character class is a character shorter than the lookahead — try it before reaching for (?!…).

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?

Live huntpar 6

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.

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

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.