Regex Golf Challenge

Write the shortest regex that matches every string in the green list and none in the red list. Scored by character count.

1 / 10 Challenge
0 Solved
0 Total Score
00:00 Time

Must Match

Must NOT Match

/ /
0 chars
Challenge 1 — Write a regex to match the green strings and avoid the red ones.

Your Best Scores (Local)

#ChallengeScore (chars)Time

What is Regex Golf?

Regex golf is a delightful programming puzzle: given two lists of strings — one that should match and one that should not — write the shortest possible regular expression that perfectly discriminates between them. The name comes from "code golf", where the goal is to write the shortest code that solves a problem. Unlike standard regex practice, regex golf forces you to think creatively about compact pattern representations.

How to Play

  1. Select a challenge from the numbered buttons above.
  2. Study the "Must Match" (green) and "Must NOT Match" (red) lists.
  3. Type your regex pattern in the input field. The / delimiters are added automatically.
  4. The tool tests your pattern in real time as you type. Green checkmarks mean a string matches, red X means it does not.
  5. When your regex is correct (all green checkmarks in the match list, all correct X marks in the don't-match list), the challenge is solved and your character count is recorded.

Tips and Strategies

Start with the simplest distinction

Look for a character, word, or pattern that appears in ALL strings in the match list but in NONE of the don't-match strings. That single differentiator might be your entire regex.

Use alternation for multiple patterns

The pipe character | lets you match either of two patterns. cat|dog matches both "cat" and "dog". For many challenges, a well-crafted alternation is shorter than a character class.

Anchors are your friends

^ anchors to the start and $ to the end. Using ^foo is much more specific than just foo and helps avoid false matches in the don't-match list.

Character classes over alternation for single characters

[aeiou] is shorter than a|e|i|o|u when matching individual characters. Negated classes like [^aeiou] can be even more powerful.

Quantifiers

+ (one or more), * (zero or more), ? (zero or one), and {n,m} (specific counts) can replace repeated characters. a{3} is shorter than aaa.

Lookaheads for complex constraints

Lookaheads (?=...) and negative lookaheads (?!...) let you assert a condition without consuming characters. This is powerful when you need to match a position based on what follows.

Famous Regex Golf Puzzles

The regex golf concept was popularized by a 2013 xkcd comic and a subsequent blog post by Peter Norvig who wrote an algorithm to automatically solve regex golf puzzles using greedy set cover. Norvig's solutions are often unreadable but highly optimal. The challenges in this tool are designed to be solvable by humans with reasonable effort, ranging from beginner (single character class) to advanced (lookaheads required).

Frequently Asked Questions

Regex golf is a programming puzzle where you write the shortest possible regular expression that matches every string in a 'match' list and fails to match every string in a 'don't match' list. It's a fun way to sharpen regex skills, similar to code golf where the goal is the shortest code.
This tool uses JavaScript's built-in RegExp engine (ECMAScript regex). It supports all standard regex features including lookaheads, backreferences, character classes, quantifiers, and alternation. It does not support PCRE-specific features like possessive quantifiers or atomic groups.
Your score for each challenge is the character length of the regex pattern (excluding the surrounding slashes and flags). Lower is better. The total score across all completed challenges is shown in the leaderboard. Completing all 10 challenges with a combined score of under 100 characters is considered expert level.
No, flags (i, g, m, etc.) are not counted in the score. Only the pattern itself is measured. However, be careful: if you use the 'i' (case-insensitive) flag, make sure it does not cause the regex to accidentally match strings in the 'don't match' list.
Yes. JavaScript's regex engine supports lookaheads (?=...) and (?!...), lookbehinds (?<=...) and (?