Custom Regex Pattern Generator

Generate accurate regular expressions using positive and negative examples.

Prompt · 3 variables

I need a regular expression to use in {{used language}}. Instead of describing the requirements in words, I will provide positive and negative examples below.

Please create a single regular expression that matches all items in "Passing Test Examples" and none of the items in "Examples to Filter". Your task is to deduce the underlying rules from the differences between these two sets.

Please present the output in the following order: the single-line regex, a breakdown table explaining each token, a 2-3 line working code snippet for {{used language}}, and a validation table verifying the match status for each provided example.

If there are boundary conditions that cannot be determined solely from the examples (e.g., length limits, case sensitivity, leading/trailing whitespace), do not assume them arbitrarily; list up to 3 clarifying questions below the tables instead. Prefer readable, self-explanatory patterns over overly dense abbreviations.

Passing Test Examples: """ {{passing test example}} """

Examples to Filter: """ {{examples to filter}} """

Copy, then paste here · ChatGPT and Claude open with the prompt filled in Open in ChatGPT ↗Open in Claude ↗Open in Gemini ↗ Edit in builder Download classroom card

Why it is written this way

Context
I need a regular expression to use in {{used language}}. Instead of describing the requirements in words, I will provide positive and negative examples below.
Task
Please create a single regular expression that matches all items in "Passing Test Examples" and none of the items in "Examples to Filter". Your task is to deduce the underlying rules from the differences between these two sets.
Format
Please present the output in the following order: the single-line regex, a breakdown table explaining each token, a 2-3 line working code snippet for {{used language}}, and a validation table verifying the match status for each provided example.
Constraints
If there are boundary conditions that cannot be determined solely from the examples (e.g., length limits, case sensitivity, leading/trailing whitespace), do not assume them arbitrarily; list up to 3 clarifying questions below the tables instead. Prefer readable, self-explanatory patterns over overly dense abbreviations.
Example
Passing Test Examples: """ {{passing test example}} """
Example
Examples to Filter: """ {{examples to filter}} """

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.

Unfamiliar terms? See Aha AI: few-shot, prompt

Compared with a bad example

Common 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

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

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