Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on Cerebras Llama 3.1 70B

Stop guessing. See how professional prompt engineering transforms Cerebras Llama 3.1 70B's output for specific technical tasks.

The "Vibe" Prompt

"Hey Llama 3.1, can you write a regex for emails?"
Low specificity, inconsistent output

Optimized Version

STABLE
You are a Regular Expression Expert. Your goal is to provide concise and robust regular expressions. **Task:** Write a regular expression to match standard email addresses. The regex should ideally cover common email formats including subdomains, alphanumeric usernames, and popular top-level domains. Consider edge cases but prioritize common use. Explain your reasoning step-by-step. **Constraints:** * Must be a single regex pattern. * Avoid overly complex lookaheads/lookbacks unless absolutely necessary for correctness. **Output Format:** ```regex YOUR_REGEX_HERE ``` **Reasoning:** 1. Explain the username part. 2. Explain the '@' separator. 3. Explain the domain part. 4. Explain the TLD part. **Step-by-step thought process:** 1. **Analyze requirements:** Need to match standard email addresses, balancing robustness with complexity. 2. **Break down into components:** Username, '@', domain, TLD. 3. **Draft username pattern:** Start with `[a-zA-Z0-9._%+-]+`. This covers common characters. Need to ensure it doesn't start/end with special characters internally for basic validation, but for common use, this is a good start. 4. **Draft domain pattern:** `[a-zA-Z0-9.-]+`. Similar to username, allowing hyphens and dots. 5. **Draft TLD pattern:** `[a-zA-Z]{2,}` for at least two letters. Extend to `[a-zA-Z]{2,6}` for common TLDs or `[a-zA-Z]{2,}` for more general. 6. **Combine the parts:** `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`. 7. **Refine based on common standards:** Add a word boundary `\b` for safety. Consider stricter TLD rules. 8. **Final proposed regex:** `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b` - (Self-correction: The pipe in `[A-Z|a-z]` means 'A-Z or | or a-z', which is incorrect. Should be `[A-Za-z]`). 9. **Final polished regex (internal thought):** `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b`. This looks good. It's robust for common cases without being overly strict for less common but valid email structures.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages several best practices for LLM interaction: 1. **Explicit Role Assignment:** 'You are a Regular Expression Expert' sets the persona and expectations for the model's output quality. 2. **Clear Goal:** Defines the primary objective ('provide concise and robust regular expressions'). 3. **Detailed Task Description:** 'standard email addresses,' 'subdomains,' 'alphanumeric usernames,' 'popular top-level domains' provides necessary context. 4. **Constraints:** 'single regex pattern,' 'avoid overly complex' guide the model towards a practical solution. 5. **Chain-of-Thought (CoT):** The 'Step-by-step thought process' section models how a human expert would approach the problem, guiding the LLM to think systematically and reducing the likelihood of superficial answers. This includes self-correction. 6. **Structured Output:** 'Output Format' ensures the regex is easily extractable, and 'Reasoning' sections provide clear explanations. 7. **Example Reasoning Structure:** By asking for specific points in the reasoning (username, '@', domain, TLD), the prompt ensures a comprehensive explanation.

-200%
Token Efficiency Gain
The optimized prompt explicitly defines the model's role as a 'Regular Expression Expert'.
The optimized prompt uses a 'Step-by-step thought process' to guide the model's reasoning.
The 'vibe_prompt' is significantly shorter but lacks structure and specific guidance.

Ready to stop burning tokens?

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

Optimize My Prompts