Regular expressions have a reputation for being unreadable, and honestly, they've earned it. But the core of regex is about six ideas, and once those click, most patterns go from cryptic to mechanical.

The six ideas that matter

  • Literals match themselves: cat matches "cat". Most of every pattern is just literal text.
  • Character classes match one of a set: [aeiou] matches any vowel, [0-9] any digit. Shorthands like \d (digit), \w (word character), and \s (whitespace) cover the common cases.
  • Quantifiers say how many: * (zero or more), + (one or more), ? (zero or one), {2,4} (between two and four).
  • Anchors pin the position: ^ (start), $ (end), \b (word boundary). Without anchors, the pattern can match anywhere inside the text.
  • Groups capture and organize: (...) captures a part for extraction or back-references; (?:...) groups without capturing.
  • Alternation offers choices: cat|dog matches either word.

Reading a real pattern

Take ^[A-Z][a-z]+@[a-z]+\.[a-z]{2,}$. Piece by piece: start of string, one uppercase letter, one or more lowercase letters, a literal @, one or more lowercase letters, a literal dot, then two or more lowercase letters to the end. It's a (simplified) capitalized-email matcher — and reading it left to right is exactly how the engine executes it.

Related reading: How to Remain Valuable When Intelligence Becomes Cheap — a 224-page practical book on staying valuable as intelligence gets cheap. $3.84. Read it on Gumroad →

Test, don't squint

The fastest way to learn regex is to run patterns against real text and watch what matches. The regex tester & explainer highlights matches live and explains each token, while the advanced regex extractor pulls matches out of larger documents. Paste the pattern above into either one and modify it — breaking patterns on purpose is genuinely the best teacher.

Two warnings worth heeding

First, regex is the wrong tool for nested structures like HTML or JSON — use a parser. Second, a pattern with nested quantifiers like (a+)+ can hang on hostile input (catastrophic backtracking), so keep user-facing patterns simple. Within those guardrails, regex remains the fastest text filter ever built into every language you use.