Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Regular expression writing
on Claude 3.5 Sonnet

Stop guessing. See how professional prompt engineering transforms Claude 3.5 Sonnet's output for specific technical tasks.

The "Vibe" Prompt

"Help me write a regular expression. I need to match email addresses that end with either '.com' or '.org'. They should have a username part with letters, numbers, dots, or underscores, and a domain name part with letters or numbers, followed by '.com' or '.org'."
Low specificity, inconsistent output

Optimized Version

STABLE
{ "task": "Regular Expression Generation", "constraint_set": [ { "constraint_type": "match_specific_endings", "values": [ ".com", ".org" ], "description": "The email address must end with either '.com' or '.org'." }, { "constraint_type": "username_format", "allowed_characters": "alphanumeric, dot, underscore", "description": "The username part (before '@') can contain letters, numbers, dots ('.'), or underscores ('_')." }, { "constraint_type": "domain_format", "allowed_characters": "alphanumeric", "description": "The domain name part (between '@' and the TLD) can contain letters or numbers." }, { "constraint_type": "email_structure", "pattern": "username@domain.tld", "description": "Follows standard email structure." } ], "example_inputs": [ "test@example.com", "user.name_123@domain42.org", "invalid@domain.net" ], "expected_outputs": [ "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-]+)\.(com|org)$", "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-]+)\.(com|org)$", null ], "logic_chain": [ { "step": 1, "description": "Define the overall email structure: username@domain.tld." }, { "step": 2, "description": "Specify the character set for the username: alphanumeric, dot, underscore. Ensure it can appear one or more times." }, { "step": 3, "description": "Specify the character set for the domain: alphanumeric. Ensure it can appear one or more times." }, { "step": 4, "description": "Specify the allowed top-level domains (TLDs): .com or .org. Use an alternation group." }, { "step": 5, "description": "Anchor the regex to the start (^) and end ($) of the string for exact matches." }, { "step": 6, "description": "Combine these components into a single regular expression, ensuring proper escaping of special characters like '.' and '@'." } ] }
Structured, task-focused, reduced hallucinations

Engineering Rationale

The 'optimized_prompt' works better due to its highly structured, machine-readable format utilizing JSON. This eliminates ambiguity by explicitly defining constraints, allowed characters, and providing clear examples with expected outputs. The 'logic_chain' guides the model through the precise steps required to construct the regular expression, mimicking a human thought process and reducing the chance of misinterpretation. It also includes explicit positive and negative examples (via 'expected_outputs' including 'null' for non-matches), which gives the model clear validation criteria. The constraint_set field ensures all requirements are explicitly listed, preventing the model from overlooking details.

5%
Token Efficiency Gain
The optimized prompt explicitly defines all character sets and patterns.
The optimized prompt provides clear positive and negative examples for evaluation.
The `logic_chain` in the optimized prompt guides the AI through the regex construction process.

Ready to stop burning tokens?

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

Optimize My Prompts