Regex101
02 / 02

Regex101: Named Groups, Lookaheads & Building Patterns

Regex101: Named Groups, Lookaheads & Building Patterns

Named Capture Groups

Pattern: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

// match.groups.year instead of match[1] -- more readable,
// and less fragile if group order later changes

Lookaheads: Zero-Width Assertions

Pattern: \d+(?=px)
Input:   width: 100px; height: 50em;

Matches: 100  (not 50 -- "em" isn't "px")

// The lookahead checks what follows WITHOUT including
// it in the actual match

Catastrophic Backtracking

Certain pattern structures (especially nested quantifiers like (a+)+) can cause the engine to explore an exponential number of backtracking paths on specific pathological inputs -- effectively hanging. Worth understanding before shipping a complex pattern.

Building Patterns Incrementally

Start with a simple pattern matching part of the target text, then incrementally add complexity while continuously checking against representative test cases -- rather than attempting the full complex pattern in one untested pass.

A Tool, Not a Substitute for Understanding

A testing tool accelerates the iterate-test-debug loop and explains existing patterns, but a developer still needs real regex knowledge to write a sensible starting pattern, interpret results correctly, and recognize when a pattern's whole approach is flawed.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free