| name | regex-pattern-builder |
| description | Builds and explains regex patterns from natural language, tests patterns, and provides examples. Use when user asks to "create regex", "regex pattern", "match pattern", "validate email/phone", or "regex help". |
| allowed-tools | ["Read","Write"] |
Regex Pattern Builder
Creates regex patterns from natural language descriptions, explains existing patterns, and helps test and debug regex.
When to Use
- "Create a regex to match emails"
- "Regex pattern for phone numbers"
- "How do I match URLs"
- "Explain this regex"
- "Test my regex pattern"
- "Validate password regex"
Instructions
1. Understand the Requirement
Ask clarifying questions if needed:
- What format are you trying to match?
- Should it be strict or permissive?
- What language/flavor (JavaScript, Python, etc.)?
- Full match or contains?
- Case sensitive?
2. Build Pattern from Description
Common Patterns
Email validation:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Phone numbers:
/^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$/
/^\+?[1-9]\d{1,14}$/
URLs:
/^https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=]+$/
/^(https?):\/\/([\w.-]+)(:\d+)?(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?$/
Passwords:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Dates:
/^\d{4}-\d{2}-\d{2}$/
/^(0?[1-9]|1[0-2])\/(0?[1-9]|[12]\d|3[01])\/\d{4}$/
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/
Credit card:
/^[\d\s-]{13,19}$/
IP addresses:
/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/
/^(\d{1,3}\.){3}\d{1,3}$/
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
Usernames:
/^[a-zA-Z0-9_-]{3,16}$/
/^[a-zA-Z][a-zA-Z0-9_-]{2,15}$/
Hex colors:
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{4})$/
HTML tags:
/<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/
/<[^>]*>/g
/<div\b[^>]*>(.*?)<\/div>/gs
File paths:
/^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/
/^\/(?:[^\/\0]+\/)*[^\/\0]*$/
/\.([a-zA-Z0-9]+)$/
3. Provide Test Cases
For each pattern, show examples:
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
console.log(emailRegex.test('user@example.com'))
console.log(emailRegex.test('first.last@company.co.uk'))
console.log(emailRegex.test('user+tag@domain.org'))
console.log(emailRegex.test('@example.com'))
console.log(emailRegex.test('user@'))
console.log(emailRegex.test('user example.com'))
console.log(emailRegex.test('user@domain'))
4. Explain Pattern Components
Basic syntax:
. - Any character except newline
\d - Digit [0-9]
\D - Not digit
\w - Word character [a-zA-Z0-9_]
\W - Not word character
\s - Whitespace [\t\n\r ]
\S - Not whitespace
^ - Start of string/line
$ - End of string/line
\b - Word boundary
\B - Not word boundary
* - 0 or more (greedy)
+ - 1 or more (greedy)
? - 0 or 1 (greedy)
{n} - Exactly n times
{n,} - n or more times
{n,m} - Between n and m times
*? - 0 or more (lazy)
+? - 1 or more (lazy)
?? - 0 or 1 (lazy)
[abc] - Any of a, b, or c
[^abc] - Not a, b, or c
[a-z] - Any lowercase letter
[0-9] - Any digit
(...) - Capture group
(?:...) - Non-capturing group
(?=...) - Positive lookahead
(?!...) - Negative lookahead
(?<=...)- Positive lookbehind
(?<!...)- Negative lookbehind
| - OR
\ - Escape special character
5. Language-Specific Variations
JavaScript:
const regex = /pattern/gi
'text'.match(/pattern/g)
'text'.matchAll(/pattern/g)
'text'.search(/pattern/)
'text'.replace(/pattern/g, 'new')
/pattern/.test('text')
/pattern/.exec('text')
Python:
import re
re.IGNORECASE
re.MULTILINE
re.DOTALL
re.VERBOSE
re.match(pattern, string)
re.search(pattern, string)
re.findall(pattern, string)
re.finditer(pattern, string)
re.sub(pattern, repl, string)
re.split(pattern, string)
PHP:
preg_match($pattern, $subject)
preg_match_all($pattern, $subject)
preg_replace($pattern, $replace, $subject)
preg_split($pattern, $subject)
/pattern/i
/pattern/m
/pattern/s
/pattern/x
6. Common Use Cases
Extract data:
const text = "Contact: john@example.com or jane@company.org"
const emails = text.match(/[^\s@]+@[^\s@]+\.[^\s@]+/g)
console.log(emails)
Validate input:
function validateEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return regex.test(email)
}
Replace content:
const text = "Call us at 555-1234 or 555-5678"
const censored = text.replace(/\d{3}-\d{4}/g, 'XXX-XXXX')
console.log(censored)
Parse structured data:
const log = "2024-01-15 ERROR: Failed to connect"
const match = log.match(/^(\d{4}-\d{2}-\d{2}) (\w+): (.+)$/)
if (match) {
const [, date, level, message] = match
console.log({ date, level, message })
}
7. Advanced Patterns
Lookaheads and lookbehinds:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/
/\b\w+\b(?!\s+\w+)/
/(?<=\$)\d+(\.\d{2})?/
Capture groups:
const text = "John Doe (john@example.com)"
const regex = /(\w+)\s+(\w+)\s+\(([^)]+)\)/
const [, firstName, lastName, email] = text.match(regex)
console.log({ firstName, lastName, email })
Named groups (modern):
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const match = '2024-01-15'.match(regex)
console.log(match.groups)
8. Performance Tips
Avoid catastrophic backtracking:
/(a+)+b/
/a+b/
Be specific:
/<.*>/
// ✅ GOOD: Lazy quantifier
/<.*?>/
// ✅ BETTER: Specific negation
/<[^>]*>/
Anchor patterns:
/\d{3}-\d{4}/
/^\d{3}-\d{4}$/
9. Testing Tools
Provide testing code:
function testRegex(pattern, testCases) {
console.log(`Testing: ${pattern}\n`)
testCases.forEach(({ input, expected }) => {
const result = pattern.test(input)
const status = result === expected ? '✓' : '✗'
console.log(`${status} "${input}" -> ${result} (expected ${expected})`)
})
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
testRegex(emailRegex, [
{ input: 'user@example.com', expected: true },
{ input: 'invalid@', expected: false },
{ input: '@example.com', expected: false },
{ input: 'user example.com', expected: false }
])
10. Common Mistakes
Forgetting to escape:
/user.example.com/
/user\.example\.com/
Not anchoring:
/\d{3}-\d{4}/
/^\d{3}-\d{4}$/
Greedy vs lazy:
const html = '<div>content</div><span>more</span>'
html.match(/<.*>/)
html.match(/<.*?>/)
Case sensitivity:
/^[a-z]+$/
/^[a-z]+$/i
Regex Cheat Sheet
Character classes:
\d = [0-9]
\D = [^0-9]
\w = [a-zA-Z0-9_]
\W = [^a-zA-Z0-9_]
\s = [ \t\n\r\f\v]
\S = [^ \t\n\r\f\v]
Quantifiers:
* = {0,∞}
+ = {1,∞}
? = {0,1}
{n} = exactly n
{n,} = n or more
{n,m} = between n and m
Anchors:
^ = start of string/line
$ = end of string/line
\b = word boundary
\A = start of string (Python)
\Z = end of string (Python)
Groups:
(...) = capture
(?:...) = non-capture
(?<name>...) = named capture
(?=...) = lookahead
(?!...) = negative lookahead
(?<=...) = lookbehind
(?<!...) = negative lookbehind