Skip to main content
Module 6 · Lesson 2 of 422/32 in the course~16 min
Module lessons (2/4)

Negative lookahead: `(?!...)`

Lesson preview5 hands-on exercises readyRegex Course · Module 6 — Lookaround

Check that a certain pattern does NOT follow the current position.

What you'll learn in this lesson

  • Write lookahead negative assertions (?!...) to exclude matches followed by certain prefixes, suffixes, or words.
  • Combine ^ + (?!...) to filter entire rows that do not contain a prohibited term.
  • Avoid the classic trap of partial backtracking of \d+(?! euro) by anchoring the token with \b.

Overview

The negative lookahead (?!...) is the opposite of the positive one: it expects the specified pattern to NOT appear immediately after the current position. This assertion is also zero-width and does not consume any characters.

When the engine encounters (?!...), it tries to see if the forbidden pattern matches immediately after the current point: if it matches, the assertion fails and that match is discarded; if it doesn't match, the assertion is successful!

Sections covered in this lesson

  1. 1Mental model: the veto before confirming the match
  2. 24 progressive examples step by step
  3. 3Guided practical exercises
  4. 4Quick check
Pro Lesson · Module 6 — Lookaround

Unlock "Negative lookahead: `(?!...)`" and the full Regex Course curriculum

Check that a certain pattern does NOT follow the current position.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Regex Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.