Regex Pattern Builder and Tester

Generate, explain, and test regular expression patterns for common and complex text matching scenarios with step-by-step breakdowns and edge case handling.

Prompt Template

You are a regex expert. Help me build and understand a regular expression pattern.

**What I need to match:**
[Describe what you want to match — e.g., "Extract all email addresses from a block of text" or "Validate phone numbers in international format" or "Parse CSV fields that may contain quoted commas"]

**Programming language/engine:**
[e.g., JavaScript, Python, PCRE, .NET, Go]

**Sample input text:**
```
[Paste sample text here]
```

**Expected matches:**
[List what should match and what should NOT match]

Please provide:
1. **The regex pattern** with appropriate flags
2. **Step-by-step breakdown** — explain each component of the pattern in plain English
3. **Visual railroad diagram** description of the pattern flow
4. **Test cases table** — 10+ test strings showing match/no-match with the captured groups
5. **Edge cases** — tricky inputs that might break the pattern and how to handle them
6. **Performance notes** — any backtracking risks, and optimization suggestions
7. **Code snippet** — ready-to-use implementation in the specified language
8. **Common variations** — 2-3 alternative patterns for stricter or looser matching

Example Output

# Regex: Email Address Extractor

Pattern

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

Flags: `g` (global), `i` (case-insensitive)

Step-by-Step Breakdown

| Component | Meaning |

|-----------|--------|

| `[a-zA-Z0-9._%+-]+` | Local part: one or more alphanumeric chars, dots, underscores, percent, plus, hyphen |

| `@` | Literal @ symbol |

| `[a-zA-Z0-9.-]+` | Domain: one or more alphanumeric chars, dots, hyphens |

| `\.` | Literal dot before TLD |

| `[a-zA-Z]{2,}` | TLD: two or more letters (com, org, co.uk) |

Test Cases

| Input | Match? | Captured |

|-------|--------|----------|

| `user@example.com` | ✅ | `user@example.com` |

| `first.last+tag@sub.domain.co.uk` | ✅ | `first.last+tag@sub.domain.co.uk` |

| `user@.com` | ❌ | — |

| `@example.com` | ❌ | — |

| `user@example` | ❌ | — |

| `user name@example.com` | ❌ (space) | — |

JavaScript Implementation

const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/gi;

const matches = text.match(emailRegex) || [];

console.log(`Found ${matches.length} emails:`, matches);

Edge Cases

- **Quoted local parts** (`"user name"@example.com`): Valid per RFC 5321 but rare — add `"[^"]+"` alternative if needed

- **IP domain** (`user@[192.168.1.1]`): Not matched by default — extend domain part if required

- **Internationalized emails** (`user@例え.jp`): Requires Unicode pattern support

Tips for Best Results

  • 💡Always specify the regex engine — JavaScript, Python, and PCRE have different feature support
  • 💡Test with at least 10 edge cases including empty strings, special characters, and Unicode
  • 💡Avoid catastrophic backtracking by using possessive quantifiers or atomic groups when available
  • 💡Use named capture groups (?P<name>...) in Python or (?<name>...) in JS for readable code
  • 💡For production validation, prefer dedicated libraries over regex (e.g., email-validator for emails)