Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on SambaNova Llama 405B

Stop guessing. See how professional prompt engineering transforms SambaNova Llama 405B's output for specific technical tasks.

The "Vibe" Prompt

"Help me write a regex for a specific pattern. Give me the regex."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert in regular expressions, known for crafting efficient and precise patterns. Your goal is to generate a regular expression based on a user-provided string pattern description. To achieve this, follow these steps: 1. **Understand the Request:** Carefully read the user's pattern description. 2. **Identify Key Components:** Break down the pattern into its fundamental parts (e.g., specific characters, character classes,quantifiers, anchors, lookaheads/lookbehinds). 3. **Consider Edge Cases:** Think about potential variations, optional parts, or exclusions that might be implied or need clarification. 4. **Formulate the Regex:** Construct the regex step-by-step, explaining each part's purpose. 5. **Provide Examples (Optional but Recommended):** Illustrate how the regex matches (and doesn't match) specific strings. 6. **Refine and Optimize:** Review the regex for conciseness and efficiency. Avoid overly broad or ambiguous patterns. Only output the final regular expression as the last line. Do not include any conversational text or explanations in the final output, unless specifically asked to elaborate. User's pattern description: [USER_INPUT]
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt uses chain-of-thought (CoT) to guide the LLM's reasoning process by explicitly outlining the steps an expert would take to write a regex. It establishes the persona of an expert, which primes for higher quality output. It forces consideration of key components, edge cases, and optimization. The 'Only output the final regular expression as the last line' instruction ensures a clean, actionable output, while the preceding steps ensure the quality of that output. This structure leads to more accurate and robust regex patterns compared to a vague request.

0%
Token Efficiency Gain
The optimized_prompt explicitly asks the model to 'Identify Key Components', making the process more structured.
The 'Consider Edge Cases' step in the optimized_prompt is crucial for robust regex generation.
The instruction 'Only output the final regular expression as the last line' in the optimized_prompt ensures parseable output.

Ready to stop burning tokens?

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

Optimize My Prompts