C.W.K.
Stream
Lesson 12 of 12 · published

Regex Puzzles — Test Your Mastery

~15 min · puzzles, practice, graduation

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Five puzzles, increasing difficulty

If you can solve all five, you've internalized this quest. Try each in your head first; check the answer at the bottom only after you have a candidate.

Puzzle 1: Match palindromes of length 3-5

Words that read the same forwards and backwards: 'mom', 'eye', 'level', 'civic'. Use word boundaries.

Puzzle 2: Match 'fizz' but not when preceded by 'buzz'

Find 'fizz' in text, but skip occurrences where 'buzz' immediately precedes (with optional space).

Puzzle 3: Match strings with EXACTLY two vowels

'cat' (1), 'beat' (2), 'beats' (2), 'beautiful' (5). Only 'beat' and 'beats' should match.

Puzzle 4: Strip ANSI color codes

Terminal output often has codes like \x1b[31m or \x1b[1;32m. Remove them all without removing other content.

Puzzle 5: Find duplicate adjacent words

Detect things like 'the the cat' or 'sat sat'. Return the duplicated word.

Spoilers (after you've tried)

1. \b(\w)(\w?)(\w)?\3\2?\1\b — capturing pairs around the center, using backreferences and optional matching.

2. (?<!buzz\s?)fizz — negative lookbehind. (Variable-width lookbehind needed; Python regex module or fixed-width pad.)

3. ^[^aeiou]*[aeiou][^aeiou]*[aeiou][^aeiou]*$ — non-vowels, vowel, non-vowels, vowel, non-vowels.

4. \x1b\[[\d;]*m — escape, bracket, digits and semicolons, m.

5. \b(\w+)\s+\1\b — backreference to detect repetition.

The graduation

If you understood all five — including the trade-offs (Puzzle 2's lookbehind variability, Puzzle 1's center-character handling) — you've graduated. The rest is practice. Keep regex101 bookmarked, treat lookaround as a power tool not a default, and remember the Golden Rule: smallest tool that solves the shape problem.

Code

Puzzle solutions·python
import re

# Puzzle 1: 3-5 letter palindromes
PALINDROME = re.compile(r'\b(\w)(\w?)(\w)?\3?\2?\1\b')
for m in PALINDROME.finditer('mom eye level civic noon racecar test'):
    word = m.group()
    if word == word[::-1] and 3 <= len(word) <= 5:
        print(word)
# mom
# eye
# level
# civic
# noon

# Puzzle 2: 'fizz' not preceded by 'buzz'
# Python re needs fixed-width lookbehind
re.findall(r'(?<!buzz )fizz', 'buzz fizz solo fizz buzzfizz')
# ['fizz', 'fizz']  — middle and end

# Puzzle 3: exactly two vowels
TWO_VOWELS = re.compile(r'^[^aeiou]*[aeiou][^aeiou]*[aeiou][^aeiou]*$')
for word in ['cat', 'beat', 'beats', 'beautiful']:
    if TWO_VOWELS.fullmatch(word):
        print(word)
# beat
# beats

# Puzzle 4: strip ANSI codes
ANSI = re.compile(r'\x1b\[[\d;]*m')
clean = ANSI.sub('', '\x1b[31mhello\x1b[0m \x1b[1;32mworld\x1b[0m')
print(clean)  # 'hello world'

# Puzzle 5: duplicate adjacent words
DUPS = re.compile(r'\b(\w+)\s+\1\b')
for m in DUPS.finditer('the the cat sat sat by'):
    print(m.group(1))
# the
# sat

External links

Exercise

Solve all five puzzles without looking at the spoilers. For each one, write your candidate regex, test it on the example inputs, and verify before peeking. If you solve 4/5 first try, you've earned the regex-master achievement.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.