Build and Explain a Regex
Purpose
Produce a pattern that matches exactly what is intended in the engine where it will run, proven by explicit positive and negative cases, readable by the next developer and safe against pathological input.
When to use
- Validating, extracting, searching or replacing text with a pattern.
- An existing regex must be understood, fixed or made faster.
- A validation rule from requirements (codes, identifiers, formats) must become a pattern.
When not to use
- The format has a real parser or standard library function (URLs, email per RFC 5322, dates, JSON, HTML). Recommend the parser instead.
- The need is a data quality rule across a dataset. Use
data-quality-rules.
- A general walkthrough of surrounding code. Use
code-explanation.
Inputs
Required:
- What must match and what must not, ideally with real samples.
- The regex engine or language (for example PCRE, JavaScript, .NET, Java, Python
re, RE2, POSIX, database dialect).
Optional, improves quality:
- Whether it validates a whole string or finds matches inside text, case sensitivity, Unicode needs, multiline input, the capture groups needed, performance constraints (untrusted input, size).
If the engine is unknown, ask; if the user cannot say, write for a common subset and mark engine-specific features [ENGINE-DEPENDENT].
Process
- Restate the matching rule precisely: allowed characters, lengths, fixed parts, optional parts, separators, and where the match may occur.
- Build the test table first: at least five should-match and five should-not-match cases, including boundaries (min/max length, leading/trailing spaces, adjacent text, Unicode, empty).
- Decide anchoring:
^...$ or \A...\z for full validation (note $ may match before a trailing newline in some engines); word boundaries or lookarounds for extraction.
- Write the simplest pattern that satisfies the table; prefer explicit character classes over
., bounded quantifiers over */+ where lengths are known.
- Use named or non-capturing groups deliberately; capture only what the caller needs.
- Check for catastrophic backtracking: nested quantifiers, overlapping alternations,
(.*)*-like shapes; rewrite with atomic groups, possessive quantifiers or unambiguous classes if the engine supports them, or state that input length must be limited.
- Apply flags consciously: case-insensitive, multiline, dotall, Unicode; say which are required.
- Run the test table against the pattern mentally, case by case, and fix until every row passes; report any case you are unsure about as
[VERIFY].
- Give the pattern as a string literal escaped correctly for the target language, plus a commented (verbose) version if the engine supports it.
- For explaining or fixing an existing regex: break it into tokens, state what it actually matches, show a counterexample for each bug, then give the corrected version with the same test table.
- If the goal continues, suggest
unit-test-writing to turn the test table into automated tests, or secure-code-review if the pattern guards untrusted input.
Output format
# Regex: <purpose>
Engine: <engine> · Mode: <validate whole string / extract> · Flags: <...>
```<language>
<pattern as a correctly escaped literal>
```
## Breakdown
| Part | Meaning |
## Test Cases
| Input | Expected | Result |
## Performance and Safety
- Backtracking risk: <none / mitigated by ...>
## Notes and Limits
- ...
Quality checklist
Common pitfalls
- Forgetting anchors so a "validation" pattern accepts
abcINV-2024-000123xyz.
- Using
.* greedily across a line and capturing far more than intended.
- Writing lookbehind or possessive quantifiers for an engine that does not support them.
- Trying to validate email or HTML fully with a regex instead of a proper parser plus a pragmatic check.
Example
Input: "Extract invoice numbers like INV-2024-000123 from email subjects, JavaScript."
Weak: /INV.*\d+/ (matches INVALID code 42 and swallows the rest of the subject).
Strong excerpt:
/\bINV-(\d{4})-(\d{6})\b/g
| Input |
Expected |
Result |
Re: INV-2024-000123 overdue |
match INV-2024-000123 |
pass |
XINV-2024-000123 |
no match |
pass |
INV-24-000123 |
no match |
pass |
1---2name: regex-builder3description: "Writes a regular expression for a stated matching need in the target engine's dialect, with a plain-language breakdown, a table of should-match and should-not-match test cases, anchoring and escaping decisions, and a check for catastrophic backtracking. Also explains or fixes an existing regex. Use when someone needs a pattern to validate, extract, search or replace text, pastes a regex and asks what it does, or reports a regex that matches too much, too little or runs slowly."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/regex-builder7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: coding12 title: "Build and explain a regex"13 related: "code-explanation, unit-test-writing, data-quality-rules, secure-code-review, business-rules-catalog"14 prompt: "Write a regex that extracts invoice numbers like INV-2024-000123 from email subjects; we use it in JavaScript."15---1617# Build and Explain a Regex1819## Purpose20Produce a pattern that matches exactly what is intended in the engine where it will run, proven by explicit positive and negative cases, readable by the next developer and safe against pathological input.2122## When to use23- Validating, extracting, searching or replacing text with a pattern.24- An existing regex must be understood, fixed or made faster.25- A validation rule from requirements (codes, identifiers, formats) must become a pattern.2627## When not to use28- The format has a real parser or standard library function (URLs, email per RFC 5322, dates, JSON, HTML). Recommend the parser instead.29- The need is a data quality rule across a dataset. Use `data-quality-rules`.30- A general walkthrough of surrounding code. Use `code-explanation`.3132## Inputs33Required:34- What must match and what must not, ideally with real samples.35- The regex engine or language (for example PCRE, JavaScript, .NET, Java, Python `re`, RE2, POSIX, database dialect).3637Optional, improves quality:38- Whether it validates a whole string or finds matches inside text, case sensitivity, Unicode needs, multiline input, the capture groups needed, performance constraints (untrusted input, size).3940If the engine is unknown, ask; if the user cannot say, write for a common subset and mark engine-specific features `[ENGINE-DEPENDENT]`.4142## Process431. Restate the matching rule precisely: allowed characters, lengths, fixed parts, optional parts, separators, and where the match may occur.442. Build the test table first: at least five should-match and five should-not-match cases, including boundaries (min/max length, leading/trailing spaces, adjacent text, Unicode, empty).453. Decide anchoring: `^...$` or `\A...\z` for full validation (note `$` may match before a trailing newline in some engines); word boundaries or lookarounds for extraction.464. Write the simplest pattern that satisfies the table; prefer explicit character classes over `.`, bounded quantifiers over `*`/`+` where lengths are known.475. Use named or non-capturing groups deliberately; capture only what the caller needs.486. Check for catastrophic backtracking: nested quantifiers, overlapping alternations, `(.*)*`-like shapes; rewrite with atomic groups, possessive quantifiers or unambiguous classes if the engine supports them, or state that input length must be limited.497. Apply flags consciously: case-insensitive, multiline, dotall, Unicode; say which are required.508. Run the test table against the pattern mentally, case by case, and fix until every row passes; report any case you are unsure about as `[VERIFY]`.519. Give the pattern as a string literal escaped correctly for the target language, plus a commented (verbose) version if the engine supports it.5210. For explaining or fixing an existing regex: break it into tokens, state what it actually matches, show a counterexample for each bug, then give the corrected version with the same test table.5311. If the goal continues, suggest `unit-test-writing` to turn the test table into automated tests, or `secure-code-review` if the pattern guards untrusted input.5455## Output format56````markdown57# Regex: <purpose>58Engine: <engine> · Mode: <validate whole string / extract> · Flags: <...>5960```<language>61<pattern as a correctly escaped literal>62```6364## Breakdown65| Part | Meaning |6667## Test Cases68| Input | Expected | Result |6970## Performance and Safety71- Backtracking risk: <none / mitigated by ...>7273## Notes and Limits74- ...75````7677## Quality checklist78- [ ] The pattern is written for the stated engine and escaped for the target language.79- [ ] The test table has both should-match and should-not-match rows, including boundaries.80- [ ] Anchoring matches the mode (validation vs extraction).81- [ ] No nested or ambiguous quantifiers remain without a stated mitigation.82- [ ] Cases not verified with certainty are marked `[VERIFY]`; inferred input formats are labeled `[ASSUMPTION]`.83- [ ] A parser or library alternative was considered for standard formats.84- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.8586## Common pitfalls87- Forgetting anchors so a "validation" pattern accepts `abcINV-2024-000123xyz`.88- Using `.*` greedily across a line and capturing far more than intended.89- Writing lookbehind or possessive quantifiers for an engine that does not support them.90- Trying to validate email or HTML fully with a regex instead of a proper parser plus a pragmatic check.9192## Example93Input: "Extract invoice numbers like INV-2024-000123 from email subjects, JavaScript."9495Weak: `/INV.*\d+/` (matches `INVALID code 42` and swallows the rest of the subject).9697Strong excerpt:98```javascript99/\bINV-(\d{4})-(\d{6})\b/g100```101| Input | Expected | Result |102|---|---|---|103| `Re: INV-2024-000123 overdue` | match `INV-2024-000123` | pass |104| `XINV-2024-000123` | no match | pass |105| `INV-24-000123` | no match | pass |