Learn Regular expressions

A compact pattern language for searching, validating and rewriting text

18 lessons · 3 hours · 4 levels · covers Regular expressions PCRE2 10.45, ECMAScript 2025, Python 3.13 re

Regular expressions describe sets of strings with a few dozen symbols, and nearly every language, editor and command-line tool supports them. Learn them once to search code, extract fields from logs, validate input and do bulk search-and-replace, and learn their limits so they never become a bug or a security hole.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 46 min in total
2. PracticalUse it productively at work, with the usual idioms11 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs15 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know18 lessons · 3 hours in total

The Regular expressions tree

Regular expressionsLiterals, metacharacters and escapingLiterals,metacharacters an…Character classesCharacter classesQuantifiers: * + ? and {m,n}Quantifiers: * + ?and {m,n}Anchors and word boundariesAnchors and wordboundariesAlternation and groupingAlternation andgroupingCapture groups and named groupsCapture groups andnamed groupsGreedy versus lazy quantifiersGreedy versus lazyquantifiersFlags: case, multiline, dotall, verboseFlags: case, multiline,dotall, verboseSearch and replace with groupsSearch and replacewith groupsRegex flavours: POSIX, PCRE, JS, Python, RE2Regex flavours:POSIX, PCRE, JS, P…When not to use a regexWhen not to use aregex
Essentials and Practical shown (11 of 18 lessons); the full list is below. Click a lesson to read it.

All 18 lessons, in order

Pattern basics

  1. Literals, metacharacters and escaping · Essentials · 8 min

    A regex is a pattern matched against text: most characters match themselves, a dozen metacharacters have special meaning and a backslash escapes them.

  2. Character classes · Essentials · 10 min

    Match one character from a set with [abc], ranges like [a-z], negation [^...], the shorthands \d \w \s and the dot.

  3. Quantifiers: * + ? and {m,n} · Essentials · 10 min

    Repeat the previous item with * (zero or more), + (one or more), ? (optional) and {m,n} (bounded counts).

  4. Anchors and word boundaries · Essentials · 8 min

    Pin matches to positions with ^ and $ (start and end), \b word boundaries and \A \z for the absolute string ends.

  5. Alternation and grouping · Essentials · 10 min

    Match one of several alternatives with |, and use parentheses to group parts so quantifiers and alternation apply to them.

Groups and replacement

  1. Capture groups and named groups · Practical · 12 min

    Extract parts of a match with numbered (...) and named (?P<name>...) or (?<name>...) groups, and read them from the match object.

  2. Greedy versus lazy quantifiers · Practical · 10 min

    Understand why .* overshoots, how lazy quantifiers *? +? ?? take as little as possible, and why a negated class is often better.

  3. Search and replace with groups · Practical · 12 min

    Rewrite text with re.sub and String.replace, refer to groups as \1, \g<name>, $1 or $<name>, and use a function for computed replacements.

Flags and flavours

  1. Flags: case, multiline, dotall, verbose · Practical · 10 min

    Change matching behaviour with flags: ignore case, ^/$ per line, dot matches newline, verbose patterns with comments, and JavaScript's g, y, u, v and d.

  2. Regex flavours: POSIX, PCRE, JS, Python, RE2 · Practical · 12 min

    Know the main regex dialects and how they differ in syntax and features, so a pattern copied from one place works in another.

  3. When not to use a regex · Practical · 8 min

    Recognise jobs regexes do badly, like nested structures, HTML, JSON, full email validation and dates, and reach for parsers and libraries instead.

Advanced constructs

  1. Lookahead and lookbehind · Advanced · 12 min

    Assert what comes before or after a position with (?=...), (?!...), (?<=...) and (?<!...) without consuming it.

  2. Backreferences inside the pattern · Advanced · 10 min

    Require the same text to appear twice with \1 or \k<name>, to find repeated words, matching quotes and paired tags.

  3. Unicode in regular expressions · Advanced · 12 min

    Match international text correctly: Unicode-aware \w and \d, \p{L} property classes, the JavaScript u and v flags, and graphemes versus code points.

  4. Atomic groups and possessive quantifiers · Advanced · 12 min

    Stop the engine from backtracking into a group with (?>...) or possessive quantifiers *+ ++ ?+, for speed and to rule out unwanted matches.

How engines work

  1. How a backtracking engine matches · Expert · 15 min

    Follow how PCRE-style engines try each start position, branch on quantifiers and alternatives, and backtrack on failure.

  2. Catastrophic backtracking and ReDoS · Expert · 15 min

    Spot nested or overlapping quantifiers that take exponential time on failing input, and fix them with stricter patterns, atomic groups or a linear engine.

  3. Automata engines: NFA, DFA and linear time · Expert · 15 min

    How Thompson NFA simulation and DFAs match in time linear in the input, why they drop backreferences, and how RE2, Go and Rust use them.