Regex Pattern Builder & Explainer
Builds and explains regex patterns for specific matching requirements with test cases and common pitfalls.
You are a regex expert and developer educator. Build a regular expression pattern for the following requirement: [describe what you want to match]. The target audience is developers who understand basics but need help with complex patterns. The goal is a correct, readable pattern. Format the output as structured sections: (1) the regex pattern in both JavaScript and Python syntax (noting any differences), (2) a step-by-step breakdown of each part. For example, explain that '\d{3}' means 'exactly 3 digits'. (3) 5 strings that SHOULD match (with expected capture groups highlighted), (4) 5 strings that should NOT match (with a note on why each fails), (5) common edge cases to watch for, (6) a code snippet showing the regex in use with match, search, replace, and split operations in [preferred language]. You must consider flags such as case-insensitive, multiline, and global. Avoid clever patterns. Only use named capture groups where it aids clarity. If the requirement is better solved without regex (e.g. URL parsing), suggest the alternative instead. Do not assume the input is always well-formatted. Tone should be educational and precise.Why this prompt works
The unusual feature of this prompt is the 'if the requirement is better solved without regex (e.g. URL parsing), suggest the alternative instead' clause. Most regex-builders happily produce a regex for anything you ask, even when a built-in parser or split would be clearer. This prompt nudges toward that conversation, which is the kind of judgment a senior engineer would make. The dual-language output (JavaScript and Python with noted differences) covers the two regex flavours engineers most commonly mix up. The five-strings-that-should-match plus five-that-shouldn't is the test-case format that surfaces edge cases the requester didn't think to specify.
When to reach for it
- You're writing validation logic and need a regex with explained components rather than a copy-pasted black box.
- You're sense-checking a regex you found online and want it explained step-by-step before deploying.
- You're teaching regex to junior engineers and need worked examples showing both pattern and reasoning.
How to customise it
The matching requirement is the single most important input, and being precise about edge cases up front saves rounds of iteration. 'Email addresses' is too vague; 'email addresses including +tags but excluding addresses with multiple consecutive dots' is something the model can build precisely. Specifying the language matters: JS regex and Python regex have different flag syntax and different lookbehind support, and giving both produces output that addresses both. For patterns intended for production, add 'include performance notes for catastrophic backtracking risks'; the model touches on this only if asked.
What good output looks like
The pattern in JavaScript syntax, then in Python syntax with noted differences. A breakdown of each component (e.g. '\d{3}' explained as 'exactly 3 digits'). Five matching strings with capture groups highlighted, five non-matching with reasons. An edge cases section. A code snippet showing match, search, replace, and split usage. If applicable, a 'why this is a regex problem (or isn't)' note. Total length 400 to 800 words.
Watch out for
The model can produce confident-looking regexes that are subtly wrong on edge cases, especially around quantifiers and lookbehinds. The five-non-matching-strings section is your sanity check; if any of those strings actually do match the regex when you test it, the pattern is wrong and needs revision. Don't deploy regex into production without running it against your actual input set.
Build a prompt like this for your task
Use the free guided prompt builder on the homepage: pick what you need, answer three quick questions, and get a high-scoring prompt of your own.
Open the prompt builder →More coding prompts
Code Review Feedback Generator
Reviews code for bugs, performance, security, readability, and best practices with specific line-by-line feedback.
REST API Endpoint Designer
Designs RESTful API endpoints with routes, methods, request/response schemas, auth, and error codes.
Database Schema Designer
Designs a normalised database schema with tables, relationships, indexes, and migration SQL for a given domain.