Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code GenerationintermediateNew

Regex Lookahead

Share

Write regex with lookaheads, lookbehinds, and named groups

Works with OpenClaude

You are a regex pattern expert. The user wants to write regex patterns using lookaheads, lookbehinds, and named groups to match text conditionally without consuming characters.

What to check first

  • Confirm your regex engine supports lookaheads/lookbehinds (JavaScript, Python 3.8+, PCRE, .NET all support these)
  • Test regex in an online tool like regex101.com with the correct flavor selected
  • Verify your language supports named groups syntax (most modern languages do)

Steps

  1. Identify what you need to match conditionally — the pattern that must exist but shouldn't be captured
  2. Decide if you need positive lookahead (?=...), negative lookahead (?!...), positive lookbehind (?<=...), or negative lookbehind (?<!...) based on whether you want the condition to exist or not exist
  3. Place lookahead assertions after the main pattern; place lookbehind assertions before the main pattern
  4. Define named groups using (?P<name>...) in Python or (?<name>...) in JavaScript/PCRE to label captured segments
  5. Build the main pattern between the lookahead/lookbehind assertions
  6. Test with strings where the condition is present, absent, and at edge positions
  7. Extract matches using named group accessors: .group('name') in Python or .groups.name in JavaScript
  8. Remember that lookarounds match zero characters — they assert position but don't consume input

Code

import re

# Example 1: Find prices (digits) followed by dollar sign, without capturing the $
pattern1 = r'(?P<price>\d+(?:\.\d{2})?)(?=\$)'
text1 = "The item costs 19.99$ in total"
match1 = re.search(pattern1, text1)
if match1:
    print(f"Price: {match1.group('price')}")  # Output: 19.99

# Example 2: Match words NOT preceded by "Mr. "
pattern2 = r'(?<!Mr\. )(?P<name>[A-Z][a-z]+)'
text2 = "Mr. Smith and John attended the meeting"
matches2 = re.finditer(pattern2, text2)
for match in matches2:
    print(f"Found name: {match.group('name')}")  # Output: John

# Example 3: Extract password (must be followed by @, must NOT be preceded by old_)
pattern3 = r'(?<!old_)(?P<password>[a-zA-Z0-9]{8,})(?=@)'
text3 = "new_pass12345@ and old_pass12345@"
matches3 = re.finditer(pattern3, text3)
for match in matches3:
    print(f"Valid password: {match.group('password')}")  # Output: pass12345

# Example 4: Match email domain, require .com or .org (

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
regexlookaheadpatterns

Install command:

curl -o ~/.claude/skills/regex-lookahead.md https://clskills.in/skills/code-generation/regex-lookahead.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

Want a Code Generation skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.