Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on Command R+

Stop guessing. See how professional prompt engineering transforms Command R+'s output for specific technical tasks.

The "Vibe" Prompt

"Write a regex for me."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert in regular expressions, with a deep understanding of syntax, performance, and best practices. Your task is to write a regular expression based on the user's request. Follow a chain-of-thought process to ensure accuracy and efficiency. **Chain of Thought:** 1. **Deconstruct the Request:** Identify the core elements the regex needs to match, exclude, or capture. Detail any constraints (e.g., specific delimiters, character ranges, exact counts). 2. **Identify Key Components:** Determine the specific regex constructs (e.g., character classes, quantifiers, anchors, lookarounds, alternation, grouping, backreferences) that will be necessary. 3. **Draft Initial Pattern:** Construct a preliminary regex pattern based on the identified components. 4. **Refine and Optimize:** * **Efficiency:** Look for opportunities to simplify, avoid unnecessary backtracking, or use non-greedy quantifiers if appropriate. * **Readability:** Ensure the pattern is as clear as possible, considering comments if placed in a programming context (though the output should be the raw regex). * **Specificity:** Confirm it doesn't match unwanted strings and covers all desired cases. 5. **Test Cases (Mental Walkthrough):** Briefly, in your mind, test the regex against a few positive and negative examples based on the assumed intent. 6. **Final Regex Presentation:** Present the finalized, optimized regex pattern. **User Request:** "I need a regex to validate an email address. It should allow standard alphanumeric characters, dots, underscores, and hyphens before the '@', and a domain with an alphanumeric top-level domain of 2 to 6 characters. No IP addresses or leading/trailing dots/hyphens in the local part or domain labels."
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages chain-of-thought to guide the model through a structured problem-solving process for regex creation. It establishes the model as an 'expert,' defines clear steps (deconstruct, identify components, draft, refine, test, present), and instructs it to consider specific optimization and accuracy criteria. This structured approach forces the model to think critically about the problem, leading to more robust, efficient, and accurate regex solutions compared to an unguided request. The detailed example in the user request further clarifies the expected complexity and requirements.

0%
Token Efficiency Gain
The 'vibe_prompt' is extremely general and will likely produce a generic or incorrect regex without further clarification.
The 'optimized_prompt' explicitly asks for chain-of-thought, which should lead the model to explain its reasoning and justify its regex choices.
The 'optimized_prompt' sets a persona ('expert in regular expressions'), which often improves performance in specialized tasks.

Ready to stop burning tokens?

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

Optimize My Prompts