6. Lookaround Assertions

Checking context without consuming characters

Anchors like ^, $, and \b assert something about a position without consuming characters. Lookaround is the generalization of that idea: you can assert that any arbitrary pattern does (or doesn't) appear before or after the current position — without including it in the match.

The Four Lookarounds

Syntax Name Asserts
(?=...) Positive lookahead What follows matches ...
(?!...) Negative lookahead What follows does NOT match ...
(?<=...) Positive lookbehind What precedes matches ...
(?<!...) Negative lookbehind What precedes does NOT match ...

All four are zero-width — they check a condition but don't consume any characters. The engine's position in the string doesn't advance past the lookaround.

Positive Lookahead: (?=...)

"Match X, but only if Y comes after it."

Pattern

\w+(?=\s*=)

Input

width = 100
height = 200
color: red

Matches

width = 100
height = 200
color: red

Why

\w+ matches a word, but only if it's followed by optional whitespace and an equals sign. "color" isn't followed by =, so it doesn't match. The = itself is NOT part of the match.

Lookahead for Validation

Lookaheads are commonly stacked at the start of a pattern to enforce multiple conditions simultaneously. A classic example is password validation:

Pattern

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$

What it enforces

  • (?=.*[A-Z]) — at least one uppercase letter
  • (?=.*[a-z]) — at least one lowercase letter
  • (?=.*\d) — at least one digit
  • (?=.*[!@#$%^&*]) — at least one special character
  • .{8,} — at least 8 characters total

Matches

P@ssw0rd!

Does NOT match

password
Pass1234

Each lookahead checks a condition from the start of the string. They all assert at the same position (after ^) without consuming characters, so they don't interfere with each other.

Negative Lookahead: (?!...)

"Match X, but only if Y does NOT come after it."

Pattern

\d+(?!\.\d)

Input

42 3.14 100 2.718

Matches

42 3.14 100 2.718

Why

The pattern matches digits NOT followed by a decimal point and more digits. Whole numbers match. For 3.14, the engine can't match 3 (it's followed by .1), but 14 works since 4 isn't followed by .\d.

A more practical example — match "foo" but not "foobar":

Pattern

foo(?!bar)

Input

foo foobar football

Matches

foo foobar football

Positive Lookbehind: (?<=...)

"Match X, but only if Y comes before it."

Pattern

(?<=\$)\d+(\.\d{2})?

Input

Price: $42.99, Quantity: 5

Matches

Price: $42.99, Quantity: 5

Why

The digits are matched only if preceded by a dollar sign. The 5 doesn't match because it's not preceded by $. The dollar sign itself is NOT part of the match — only the number is captured.

Lookbehind Limitations

In many regex flavors (JavaScript, Python), lookbehind patterns must be fixed-length. You can't use *, +, or {n,m} inside a lookbehind:

(?<=a+)b     # ERROR in most flavors — variable-length lookbehind
(?<=ab)c     # OK — fixed length (2 characters)
(?<=a|bc)d   # May or may not work — alternatives must be same length in some flavors

Some engines (Java, .NET, PCRE2) do support variable-length lookbehind.

Negative Lookbehind: (?<!...)

"Match X, but only if Y does NOT come before it."

Pattern

(?

Input

I am happy but she is unhappy

Matches

I am happy but she is unhappy

Why

Matches "happy" only when NOT preceded by "un".

Combining Lookarounds

You can combine lookahead and lookbehind to match text that's sandwiched between specific patterns — without including those patterns in the match.

Pattern

(?<=\[)\w+(?=\])

Input

Use [bold] and [italic] formatting

Matches

Use [bold] and [italic] formatting

Why

The lookbehind checks for [, the lookahead checks for ], and only the word between them is captured. The brackets themselves are excluded from the match.

Lookaround vs. Consuming Patterns

The key difference between lookaround and a regular pattern is that lookaround doesn't advance the engine's position. Compare:

Consuming pattern

a(b)c

Matches "abc" — all three characters are consumed. Group 1 = "b".

Lookaround pattern

(?<=a)b(?=c)

Matches only "b" — the a and c are checked but not consumed. The full match is just "b".

This matters for overlapping matches and for replacements. If you replace (?<=a)b(?=c) with X, you get aXc — only the b is replaced.

Key Takeaways

  • Lookarounds are zero-width assertions — they check context without consuming characters.
  • (?=...) and (?!...) look ahead; (?<=...) and (?<!...) look behind.
  • Stack multiple lookaheads at one position to enforce multiple conditions (common for validation).
  • Lookbehinds are often limited to fixed-length patterns.
  • Use lookaround when you need to match based on context but don't want the context in your match.

Next up: how regex engines work — NFA vs. DFA, backtracking, and performance pitfalls.