Regular expression explained
Professional structure: role, context, objective, details, steps, format, criteria and self-check. Designed for better results, not guaranteed ones: always review the answer.
Never use real personal data. If you need to, run it through the data cleaner.
## Role Act as a programmer who is an expert in regular expressions. ## Context I need to validate or find a text pattern and regular expressions confuse me. ## Objective Get a correct, explained and tested regular expression. ## Details (fill in what is in square brackets) - [what I want to find or validate] - [language or tool where I will use it] - [examples that should and should not match] ## Step-by-step instructions 1. Write the regular expression. 2. Explain each part in a table. 3. Test the expression against the examples given. 4. Warn about cases it does not cover. ## Output format Regex in a code block, explanation table, test table and limitations. ## Quality criteria - It works in the stated language. - It avoids patterns that could be very slow. - It is no more complex than needed. ## Self-check Before answering, check every quality criterion. If information is missing, ask me instead of making it up, and at the end state what you have assumed.
Objective
Get a correct, explained and tested regular expression.
When to use it
This prompt is brilliant when you are wrestling with complex string matching, data validation, or text extraction tasks where logic feels opaque. It shines when you need a robust, readable solution for form inputs, log parsing, or data cleaning. However, it is less useful for highly non-regular languages or complex parsing tasks like full HTML extraction or nested structures. Avoid relying on regex for deeply recursive patterns, as a proper parser or library is always the better choice for those complex requirements.
Worked example: input (fictional data)
Worked example: full response
Variants
Quick and dirty
Provide only the regex pattern and a brief one-sentence confirmation that it works as requested. Skip the table explanation, the detailed testing, and the limitations section. I just need a clean, copy-pasteable solution immediately for my script.
For absolute beginners
Explain the regex as if I have never seen one before. Use simple analogies for each part, avoiding overly technical jargon. Ensure the explanation breaks down the logic step-by-step so I can learn how to modify it myself later if my requirements change slightly.
Performance focused
Prioritise efficiency. Provide the regex, but specifically ensure it avoids catastrophic backtracking or inefficient quantifiers. Explain why your chosen approach is performant for large datasets or high-frequency processing, and highlight any potential bottlenecks I should watch out for when running this against massive text files.
Common mistakes
- Expecting a regex to validate email addresses perfectly.
- Not testing it with real cases.
- Ignoring the potential for catastrophic backtracking in complex nested patterns. How to avoid it: Always check for nested repetition and use atomic groups or possessive quantifiers to ensure efficiency.
- Assuming that a regex will correctly validate dates or complex numeric ranges. How to avoid it: Use regex for format structure only, and validate specific values with your programming language logic.
- Forgetting to escape special characters like dots or brackets in the input. How to avoid it: Always clarify if your input text contains literal dots or brackets that require backslash escaping.
- Using a single global regex that becomes impossible to maintain or debug. How to avoid it: Break your logic into smaller, named components or functions rather than one giant, unreadable line.
Related guide
General and assistant-specific tips
Getting the most out of it
- Paste the prompt as is and only replace what is in [brackets].
- If the answer does not fit, do not start over: ask for “shorter”, “more formal” or “as a table”.
- Always review the result before using it: AI can get facts, dates or names wrong.
Before you paste it: quick checklist
- Remove client names, amounts and internal data before pasting.
- Say who the text is for and which tone you want.
- Ask for a specific length: “120 words max” or “three bullet points”.
- Read the result aloud and fix anything you wouldn’t say yourself.
How to adapt it to your assistant
The template works the same in ChatGPT, Gemini, Copilot or Claude because it only uses plain language. If your assistant supports fixed instructions (custom instructions, Gems or projects), you can store your preferred tone and format there and paste only the variable part. On a free plan, split long requests into two or three shorter messages.