Coding & data

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

Why it is written this way

This prompt is built from 6 blocks
Context used language Task Format used language Constraints Example +1 Example +1 Material Work Output Material

Tap a block to see its paragraph

Context Material · What the AI needs to know

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.

See the Give context technique
  • Material What the AI needs to know ×3
  • Work What to do, and how ×1
  • Output What comes back, in what shape ×2

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.

Read the full explanation · 3 paragraphs

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

Missing from the bad prompt: Context · Format · Constraints · Example
Bad prompt 25 chars This prompt 902 chars Context Task Format Constraints Example Context · missing Format · missing Constraints · missing Example · missing
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

1

When Extracting Values Instead of Validating

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.

Show prompt

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}} """

2

For Find and Replace Operations

Batch editor replacements require both search and replace expressions. Always review the preview table before applying changes across your files.

Show prompt

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}} """

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.

Last updated 2026-09-02 · Found a mistake? Let us know