Regex From Examples
Writes a regex from examples of what should match and what should NOT match. Explains each part and warns about the classic gotchas.
The prompt
I need a regex. I'll give you examples. Write it, then explain it. Ask me for: - 3-5 strings that SHOULD match - 3-5 strings that should NOT match (near-misses are best) - Which regex flavor (JavaScript, Python, Go, PCRE — infer from context if I mention a language) Then output: **The regex** — one line, ready to paste. **Breakdown** — split it into parts, explain each part in plain English. Use a table: ``` Part What it matches ^ Start of string [a-z]+ One or more lowercase letters ... ``` **Test cases** — verify it matches all my "should" examples and rejects all my "shouldn't." If it fails any, say so and fix. **Gotchas** — if this regex has known pitfalls (catastrophic backtracking on certain inputs, greedy vs. lazy issues, anchoring, escape needs in different languages), name them. Rules: - Prefer readability over ultra-terse patterns. - Use named groups if the match will be consumed by code. - Anchor with `^` and `$` unless partial matches are wanted. - If the problem is genuinely regex-unfriendly (nested structures, HTML parsing), say so and suggest an alternative. ## How to use it Paste this prompt with your matching/non-matching examples. Answer the flavor question. Get a working regex + the reasoning to modify it later. ## Why it works Regex written from a vague description usually fails on edge cases the writer didn't think of. Writing FROM examples flips the process: the tests come first, the pattern is derived to satisfy them.
How to use it
I need a regex. I’ll give you examples. Write it, then explain it.
Ask me for:
- 3-5 strings that SHOULD match
- 3-5 strings that should NOT match (near-misses are best)
- Which regex flavor (JavaScript, Python, Go, PCRE — infer from context if I mention a language)
Then output:
The regex — one line, ready to paste.
Breakdown — split it into parts, explain each part in plain English. Use a table:
Part What it matches
^ Start of string
[a-z]+ One or more lowercase letters
...
Test cases — verify it matches all my “should” examples and rejects all my “shouldn’t.” If it fails any, say so and fix.
Gotchas — if this regex has known pitfalls (catastrophic backtracking on certain inputs, greedy vs. lazy issues, anchoring, escape needs in different languages), name them.
Rules:
- Prefer readability over ultra-terse patterns.
- Use named groups if the match will be consumed by code.
- Anchor with
^and$unless partial matches are wanted. - If the problem is genuinely regex-unfriendly (nested structures, HTML parsing), say so and suggest an alternative.
How to use it
Paste this prompt with your matching/non-matching examples. Answer the flavor question. Get a working regex + the reasoning to modify it later.
Why it works
Regex written from a vague description usually fails on edge cases the writer didn’t think of. Writing FROM examples flips the process: the tests come first, the pattern is derived to satisfy them.
✦ More like this
Related prompts
Book Recommender (Honest)
Recommends 3-5 books based on what you actually liked before. No 'Sapiens for the 400th time.' Includes honest reasons and what NOT to expect.
Chart Type Chooser
Given the data you have and the question you're answering, picks the right chart type and explains what to avoid. No dual-axis, no pie chart traps.
Code Refactor Helper
Refactors a function or file without changing behavior. Explains what changed and why, and flags any behavior it wasn't sure about.