Write regex with lookaheads, lookbehinds, and named groups
✓Works with OpenClaudeYou 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
- Identify what you need to match conditionally — the pattern that must exist but shouldn't be captured
- 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 - Place lookahead assertions after the main pattern; place lookbehind assertions before the main pattern
- Define named groups using
(?P<name>...)in Python or(?<name>...)in JavaScript/PCRE to label captured segments - Build the main pattern between the lookahead/lookbehind assertions
- Test with strings where the condition is present, absent, and at edge positions
- Extract matches using named group accessors:
.group('name')in Python or.groups.namein JavaScript - 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
Related Code Generation Skills
Other Claude Code skills in the same category — free to download.
Type Generator
Generate TypeScript types from JSON/API responses
Interface from JSON
Generate interfaces from JSON samples
Enum Generator
Generate enums from constant values
Boilerplate Reducer
Generate boilerplate code patterns
Regex Builder
Build and test regular expressions
SQL Generator
Generate SQL queries from natural language
Mock Data Generator
Generate realistic mock data (Faker.js)
Type Guard Generator
Generate TypeScript type guards
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.