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

Golf techniques

Anyone can write a regex that works. A hunter writes the SHORTEST one that works. This is regex golf — separate the monsters from the innocents in the fewest characters, one sharpening trick at a time, and match or beat par on every board.

Read the rules: Tips for shorter patterns in the Guide →

What you'll learn

Shorthands beat classes Classes beat alternation Anchor only when needed Backref tricks

Steps · 12

  1. 01Fewest characters winsBriefing
  2. 02Drop needless anchorsHunt
  3. 03\d beats [0-9]Hunt
  4. 04. beats a big classHunt
  5. 05A class beats alternationHunt
  6. 06Find the one differenceHunt
  7. 07Backreference for repeatsHunt
  8. 08Use a range, not a listHunt
  9. 09One dot, no classHunt
  10. 10Anchor only when you mustHunt
  11. 11Golf: ab, again and againHunt
  12. 12Toolkit completeBriefing

Regex golf: writing shorter patterns

Regex golf is a puzzle: you get two lists of words and write the shortest regex that matches every word on the first list and none on the second. The name comes from code golf, where the lowest count wins. On Regex Hunter each board has a par, the length of a known short answer, and matching or beating it earns a star.

Golf changes how you read a pattern. You stop describing the whole word and look for the one feature that separates the lists: a letter only the targets contain, a shared ending, a doubled character. Shorthands such as \d and \w, the dot, character classes, counts like {4} and the optional ? all trade length for the same meaning.

A golfed pattern only promises to work on the words in front of it. That's fine for a puzzle, and it's a good way to learn what each symbol really does. In production code, prefer the clearer pattern that also handles the inputs you haven't seen yet.

Examples

  1. /gr.y/

    Catches

    • gray
    • grey

    Spares

    • green
    • great
    • groovy

    gray|grey is 9 characters and gr[ae]y is 7; on these words the dot does the same job in 4.

  2. /\d{4}/

    Catches

    • pin 4821
    • year 1999
    • ref 20261

    Spares

    • pin 482
    • 12 to 34

    \d\d\d\d (8 characters) becomes \d{4} (5); a count in braces pays off once a shorthand repeats three times.

  3. /d$/

    Catches

    • jumped
    • walked
    • played

    Spares

    • jumps
    • walking
    • player

    ^[a-z]+ed$ describes the targets, but only they end in d, so d$ (2 characters) is enough here.

  4. /favou?rite/

    Catches

    • favorite
    • favourite

    Spares

    • favor
    • favour
    • fav

    favourite|favorite is 18 characters; favou?rite makes the u optional and needs only 10.

Common mistakes

  • Shortening past the edge. Recheck every cut against all the innocent words; one stray match and the shorter pattern no longer counts.
  • Keeping habits that cost characters. Anchors, (?:…) and [0-9] are sensible in real code, but on a golf board each one is a character you have to justify.
  • Writing before reading. Compare both lists first; the separating feature is often at the start or end of the words, not in the middle.

Read the rules: Tips for shorter patterns in the Guide →

Read more: xkcd regex golf: the puzzle behind comic 1313 →

Read more: Regex golf strategies: how to beat par →

Read more: Welcome to Regex Hunter — and the joy of regex golf →

Play today's daily →