Regex Sieve — Regex

Sift Nenek Marni's harvest sacks with a regex: 18 levels of anchors, character classes, quantifiers and capture groups. 18 levels, right in the browser.

What you learn in this game

A regular expression (regex) is a pattern for matching text. \d matches one digit, + means 'one or more', and the anchors ^ (start) & $ (end) force the pattern to cover the whole string. Without anchors, the pattern just needs to be found somewhere, and that often lets 'dirty' strings through. Write a pattern that passes every valid example and rejects the rest.

A character class [...] matches ONE character from the set inside, and can combine several ranges: [0-9a-fA-F] = a digit OR a hex letter (lower/upper). Follow it with {6} for exactly six, and a literal # in front. Literals match as-is.

\b is a word boundary: a zero-width position between a word character (\w) and a non-word one. \bcat\b matches a standalone "cat" but NOT part of "category" or "scatter". Without boundaries, the regex catches the substring inside other words.

Parentheses (…) don't just group: they capture the matched text into numbered groups (group 1, 2, 3, …). This is how you extract parts of a string: wrap each piece you want. match() returns those groups in order.

What the levels ask

  1. Write a pattern that matches the whole digit string and nothing else.
  2. Use a character class [a-z].
  3. Use the {n} counter for an exact count.
  4. No anchors: it just needs to be found anywhere.
  5. Use ? to make a character optional.
  6. Combine + with anchors so no other characters slip in.
  7. A class can combine ranges: [0-9a-fA-F].
  8. Use a negated class [^...] for 'anything except'.
  9. Use alternation (a|b|c) in a group, anchored.
  10. Use \b (word boundary) so "cat" isn't caught inside "category".
  11. Make the fractional part optional with a group + ?.
  12. Use \w and the range counter {min,max}.

And more, 18 levels in all.