Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on GPT-4o-mini

Stop guessing. See how professional prompt engineering transforms GPT-4o-mini's output for specific technical tasks.

The "Vibe" Prompt

"Write a regex to extract all email addresses from a given text."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert in regular expressions. Your task is to write a regex that accurately extracts all valid email addresses from any given input text. To achieve this, follow a step-by-step chain of thought: 1. **Analyze the structure of a typical email address**: Consider the components (username, '@', domain, top-level domain). Identify allowed characters for each part. 2. **Initial pattern for username**: Create a basic pattern for the username part, considering common characters like alphanumeric, periods, hyphens, and underscores. 3. **Refine username pattern**: Ensure the username doesn't start or end with a period, hyphen, or underscore, but can contain them internally. Handle consecutive special characters if necessary. 4. **Pattern for '@' symbol**: This is straightforward. 5. **Initial pattern for domain**: Create a basic pattern for the domain part, similar to the username but with fewer special characters usually. 6. **Refine domain pattern**: Ensure the domain allows alphanumeric characters, hyphens, and periods. It should not start or end with a hyphen or period. It must contain at least one period for the TLD. 7. **Pattern for Top-Level Domain (TLD)**: Typically 2 to 6 alphabetic characters. 8. **Combine all parts**: Assemble the patterns for username, '@', domain, and TLD into a single comprehensive regex. 9. **Consider edge cases/common variations**: Think about cases like subdomains, specific valid characters allowed by RFCs, or common invalid patterns to exclude (though full RFC compliance is often overly complex for practical extraction and may be noted). 10. **Final Regex Construction**: Present the final regex pattern, potentially with a brief explanation of its components for clarity. Now, provide the optimized regular expression to extract email addresses. Focus on a balance between accuracy and readability, leaning towards common email formats rather than obscure RFC edge cases. Wrap the regex in single quotes.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt uses a chain-of-thought approach, breaking down the complex task of writing a robust email regex into manageable, logical steps. This guides the model through the reasoning process required to construct an accurate and comprehensive regex. It explicitly defines the desired output format and provides constraints (e.g., balance accuracy/readability, common formats) which helps the model focus. The explicit 'expert' persona also encourages higher quality output. Compared to the 'vibe_prompt', which is vague, this prompt reduces ambiguity and directs the model towards a specific solution strategy, leading to better results and potentially reducing redundant token generation from self-correction or exploratory responses.

15%
Token Efficiency Gain
The 'optimized_prompt' should generate a more robust and frequently correct email regex than the 'vibe_prompt'.
The 'optimized_prompt' should explicitly state the regex within single quotes.
The reasoning steps in the 'optimized_prompt' should be followed implicitly or explicitly in the model's generation process (if it were to output its thoughts).

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts