Lookaheads and Lookbehinds

Assert patterns without including them in the match

#lookahead #lookbehind #assertions #zero-width

Lookaheads and Lookbehinds

Zero-width assertions that check for patterns without consuming characters.

Positive Lookahead (?=...)

\d+(?= dollars)

Matches numbers followed by " dollars" (but doesn't include " dollars"):

  • 50 dollars → matches 50
  • 50 euros → no match

Negative Lookahead (?!...)

\d+(?! dollars)

Matches numbers NOT followed by " dollars":

  • 50 dollars → no match
  • 50 euros → matches 50

Password Validation with Lookaheads

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

Requires:

  • At least one lowercase letter
  • At least one uppercase letter
  • At least one digit
  • At least one special character
  • Minimum 8 characters

Positive Lookbehind (?<=...)

(?<=\$)\d+

Matches numbers preceded by $ (but doesn't include $):

  • $50 → matches 50
  • €50 → no match

Negative Lookbehind (?<!...)

(?<!\$)\d+

Matches numbers NOT preceded by $:

  • $50 → no match
  • €50 → matches 50
  • 50 → matches 50

Combining Lookahead and Lookbehind

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

Matches dollar amounts with exactly 2 decimal places:

  • $50.00 → matches 50
  • $50.0 → no match
  • 50.00 → no match (missing $)

Multiple Lookaheads

^(?=.*[A-Z])(?=.*\d).{6,}$

Matches strings that:

  • Have at least one uppercase letter
  • Have at least one digit
  • Are at least 6 characters long

Word Boundaries with Lookaheads

\b\w+(?=ing\b)

Matches word stems before "ing":

  • running → matches runn
  • jumping → matches jump

Email Username Before @

\w+(?=@)

Matches email username (everything before @):

  • user@example.com → matches user

Extract Version Numbers

(?<=version )\d+\.\d+\.\d+

Matches version numbers after "version ":

  • version 1.2.3 → matches 1.2.3
  • v1.2.3 → no match

Complex Price Pattern

(?<=\$)\d{1,3}(,\d{3})*(\.\d{2})?(?=\s|$)

Matches formatted prices after $:

  • $1,234.56 → matches 1,234.56
  • $999 → matches 999
  • $1,234.5 → no match (wrong decimal format)

Exclude Specific Words

\b(?!and\b|or\b|the\b)\w+\b

Matches words except "and", "or", "the":

  • the cat → matches cat only
  • and then → matches then only

Find Numbers Not in Parentheses

(?<!\()\b\d+\b(?!\))

Matches numbers not surrounded by parentheses:

  • (100) and 200 → matches 200
  • (100) → no match

Discover another handy tool from EditPDF.pro