Why it is written this way
A common mistake when requesting a regex is relying solely on natural language descriptions. Asking for a "phone number regex" often yields a generic pattern that fails on real-world edge cases like unhyphenated numbers or area codes with different lengths. For common requests like email validation, models might simply regurgitate outdated patterns from the web.
This prompt solves that by providing two sets of examples instead of verbal descriptions. By supplying both positive and negative cases, the AI infers the precise logic from their differences. Negative examples are especially critical: most regex bugs stem from being overly permissive, and without negative constraints, the AI defaults to loose patterns. Providing a few varied rejection cases dramatically improves accuracy.
The Task explicitly instructs the AI to match all positive cases while excluding every negative case, prompting it to cross-check its work internally before answering. Specifying the target language upfront is essential because lookbehind support and escaping rules vary across environments; a pattern meant for Google Sheets or VS Code search requires a completely different syntax than one for Python.
The Format requirement for a "validation check table" is the most valuable part of this prompt. Constructing the table forces the AI to self-evaluate its pattern against each case and correct discrepancies on the spot. Finally, asking it to raise clarifying questions prevents the model from making silent, arbitrary assumptions about edge cases.
Compared with a bad example
Make a regex for phone numbers
This usually returns a rigid pattern like \d{3}-\d{3}-\d{4}, failing on unhyphenated or space-separated numbers. Conversely, a looser pattern like \d+ will match invalid inputs. In either case, edge-case failures only become apparent once tested against actual data.
Variations
When Extracting Values Instead of Validating
The "Passing Test Examples" below represent substrings that must be extracted from my data, while "Examples to Filter" must not be extracted. Please write an extraction regex for {{used language}} and provide a table explaining what each capture group captures.
Passing Test Examples: """ {{passing test example}} """
Examples to Filter: """ {{examples to filter}} """
Use this when the goal is data extraction rather than validation. Requesting a capture group breakdown ensures you can immediately use the groups in your code.
For Find and Replace Operations
Please provide a regex pattern and a corresponding replacement expression for find-and-replace in {{used language}}. Show a "Before / After" preview table illustrating how each example below will be transformed, and highlight any potential irreversible changes.
""" {{passing test example}} """
Batch editor replacements require both search and replace expressions. Always review the preview table before applying changes across your files.
Model notes
Regex syntax and flavor support vary across programming languages and tools. Always test the generated pattern against real-world data, and append any missed edge cases to your examples for refinement.
Related prompts
Last updated 2026-09-02 · Found a mistake? Let us know