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.
Must Match
Must NOT Match
Your Best Scores (Local)
| # | Challenge | Score (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
- Select a challenge from the numbered buttons above.
- Study the "Must Match" (green) and "Must NOT Match" (red) lists.
- Type your regex pattern in the input field. The
/delimiters are added automatically. - The tool tests your pattern in real time as you type. Green checkmarks mean a string matches, red X means it does not.
- 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).