Free — no signup required

Lookaheads: Peeking into the Future

3 min read

What a Lookahead Does

Imagine you are reading a sentence and you want to highlight a word — but only if the next word is "error". You read forward to check, then come back and highlight just the first word. You never actually move your position in the text; you just peeked ahead. That is exactly what a regex lookahead does.

A lookahead is a zero-width assertion: it checks a condition at the current position in the string but does not advance the match cursor or include the checked text in the result. The match result contains only what is outside the lookahead.

Positive Lookahead (?=...)

Syntax: PATTERN(?=CONDITION)

The engine matches PATTERN, then checks whether CONDITION follows at the current position. If the condition is satisfied, the match succeeds and returns only what PATTERN captured. The CONDITION text is not included.

Example — extract filename without extension:

Pattern:  \w+(?=\.log)
String:   access.log error.log system.txt
Matches:  access    error

system is not matched because it is not followed by .log.

Operational example — extract HTTP status codes only from response lines:

Pattern:  \d{3}(?= OK| Created| Accepted)
String:   200 OK
          201 Created
          500 Internal Server Error
          404 Not Found
Matches:  200, 201

The 500 and 404 lines do not match because they are not followed by the listed success phrases.

Negative Lookahead (?!...)

Syntax: PATTERN(?!CONDITION)

The engine matches PATTERN, then checks whether CONDITION does not follow. If the condition is absent, the match succeeds.

Example — match server names, skip staging hosts:

Pattern:  server-\d{2}(?!-staging)
String:   server-01-prod     → Match
          server-02-staging  → No match
          server-03-dr       → Match

Operational example — match config keys, skip commented-out keys:

Pattern:  (?!#)timeout=\d+
String:   timeout=30        → Match
          #timeout=60       → No match
          retry_timeout=5   → No match (different key)

Lookahead Chaining

You can stack multiple lookaheads at the same position to enforce several conditions simultaneously:

Pattern:  \w+(?=\d)(?=.*[A-Z])

This matches a word that is followed by a digit AND contains an uppercase letter somewhere after it. Each lookahead checks from the same cursor position independently.

Interview Tip

Interviewers frequently ask: "What is the difference between a lookahead and a capturing group?" The key distinction is consumption. A capturing group (\d+) advances the cursor past the matched characters and includes them in the match. A lookahead (?=\d+) checks the condition but leaves the cursor in place and includes nothing in the match. A follow-up question is often: "When would you use a lookahead instead of just matching and discarding the suffix?" — the answer is when you need the suffix to remain available for another part of the pattern to match, or when you are using grep/sed and cannot easily post-process capture groups.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free