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→ matches5050 euros→ no match
Negative Lookahead (?!...)
\d+(?! dollars)
Matches numbers NOT followed by " dollars":
50 dollars→ no match50 euros→ matches50
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→ matches50€50→ no match
Negative Lookbehind (?<!...)
(?<!\$)\d+
Matches numbers NOT preceded by $:
$50→ no match€50→ matches5050→ matches50
Combining Lookahead and Lookbehind
(?<=\$)\d+(?=\.\d{2})
Matches dollar amounts with exactly 2 decimal places:
$50.00→ matches50$50.0→ no match50.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→ matchesrunnjumping→ matchesjump
Email Username Before @
\w+(?=@)
Matches email username (everything before @):
user@example.com→ matchesuser
Extract Version Numbers
(?<=version )\d+\.\d+\.\d+
Matches version numbers after "version ":
version 1.2.3→ matches1.2.3v1.2.3→ no match
Complex Price Pattern
(?<=\$)\d{1,3}(,\d{3})*(\.\d{2})?(?=\s|$)
Matches formatted prices after $:
$1,234.56→ matches1,234.56$999→ matches999$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→ matchescatonlyand then→ matchesthenonly
Find Numbers Not in Parentheses
(?<!\()\b\d+\b(?!\))
Matches numbers not surrounded by parentheses:
(100) and 200→ matches200(100)→ no match
Discover another handy tool from EditPDF.pro