Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on Gemini 1.5 Pro

Stop guessing. See how professional prompt engineering transforms Gemini 1.5 Pro's output for specific technical tasks.

The "Vibe" Prompt

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

Optimized Version

STABLE
You are Gemini 1.5 Pro, an expert in regular expressions, known for writing highly efficient and accurate patterns. Your task is to extract all valid email addresses from a given text. Consider edge cases like emails at the beginning/end of lines, multiple domains, and common special characters allowed in the local part. Provide a single regular expression. Only output the regex, no additional text or explanation. Chain of Thought: 1. **Identify core components of an email address:** local part, '@' symbol, domain part. 2. **Local part rules:** Can contain alphanumeric, dots, hyphens, plus signs. Can also contain a few other special characters (e.g., `!#$%&'*+-/=?^_`{|}~`). Should not start or end with a dot or hyphen (though for robust extraction, allowing this at the very beginning/end of string might be too broad for some scenarios, but within the email itself it is restricted). 3. **Domain part rules:** Must contain at least one dot, followed by a top-level domain (TLD) of at least two characters. Subdomains are allowed. Can contain alphanumeric and hyphens. Hyphens cannot be at the beginning or end of a domain segment. TLDs are typically 2-63 characters. 4. **Quantifiers and anchors:** Use non-greedy quantifiers where appropriate. Consider word boundaries (`\b`) to prevent partial matches within other words. 5. **Construct the pattern incrementally:** - Local part: `[a-zA-Z0-9.!#$%&'*+/=?^_`{\}~-]+` - Adjust for initial/trailing dots/hyphens and common practices: `(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*)` - this is getting complex, simplify for general robust extraction. - Let's refine local part: `[a-zA-Z0-9._%+-]+` (common simplified set). - Domain part: `[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}` (simplified for common domains). - Combine: `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}` - Add word boundaries for better isolation. 6. **Final Regex Proposal (incorporating common best practices for general extraction):** `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b` ```regex \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b ```
Structured, task-focused, reduced hallucinations

Engineering Rationale

The 'optimized_prompt' works better because it leverages a chain-of-thought approach, guiding the model through the complex logic of constructing a robust regular expression for email extraction. It explicitly states the role, the task, and critical considerations (edge cases, valid characters). The step-by-step breakdown of email components (local part, domain, TLD), their rules, and the iterative construction of the regex helps the model reason effectively, leading to a more accurate and comprehensive pattern. The final output format requirement (only the regex in a fence) further refines the response.

0%
Token Efficiency Gain
The 'vibe_prompt' is too vague and likely to produce a simpler, less robust regex.
The 'optimized_prompt' explicitly asks for a single regex pattern without extra text.
The 'optimized_prompt' encourages consideration of edge cases like 'multiple domains' and 'special characters' which improves regex quality.

Ready to stop burning tokens?

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

Optimize My Prompts