WriteMyAIPromptFree, no sign-up

AI prompt for writing a regular expression

This is a ready-made AI prompt for writing a regular expression, written for Claude and free to copy. Regular expressions are write-only: they look plausible and fail on cases nobody tested. Supplying explicit should-match and should-not-match examples turns an unverifiable string into something checkable. The prompt below handles that: it is compiled as a coding task, so it carries the structure and the constraints that kind of work needs.

The prompt

Written for Claude. Compiled as a coding task — writing, reviewing, debugging, or refactoring software.

<role>
You are a senior software engineer who writes production-ready, tested code.
</role>

<context>
Language or tool: {{flavour}}. Examples that should match: {{should_match}}. Examples that should not: {{should_not_match}}.
</context>

<task>
Write a regular expression that matches {{what_to_match}}.
</task>

<constraints>
- Explain each part of the expression in order.
- Show it correctly matching every positive example and rejecting every negative one.
- Flag any catastrophic backtracking risk.
- Prefer a readable expression over a clever one.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
</constraints>

<output_format>
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
</output_format>

<approach>
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
</approach>

<before_you_start>
If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.
</before_you_start>

Now, write a regular expression that matches {{what_to_match}}.

Optional: start the reply with ``` to force the output straight into shape.

Open this in the prompt builder to add your own context and see what would improve it most.

What makes writing a regular expression hard to prompt for

Regular expressions are write-only: they look plausible and fail on cases nobody tested. Supplying explicit should-match and should-not-match examples turns an unverifiable string into something checkable.

Why this prompt works

Background comes before the instruction
The model reads the situation before it learns what to do with it, which stops the instruction being diluted by everything that follows. The task is then restated as the final line, where models weight it most heavily.
Rules a coding task assumes but nobody writes down
Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections. Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle. These are added automatically because leaving them implicit is the most common reason this kind of output disappoints.
Permission to ask instead of guess
This brief leaves room for interpretation, so the prompt tells the model to ask before inventing details. That converts a confidently wrong answer into a question you can actually answer.
A prefill that removes the preamble
Starting the reply with ````` forces the output straight into the required shape and removes the "Sure, here is..." opener entirely.

What to replace

PlaceholderWhat to put there
{{flavour}}Your flavour.
{{should_match}}Your should match.
{{should_not_match}}Your should not match.
{{what_to_match}}Your what to match.

Check the output before you use it

  • Test every negative example. Over-matching is more common than under-matching.
  • Check the flavour is right — Python, JavaScript and PCRE differ in lookbehind and named groups.
  • For anything running on user input, check the backtracking warning.

The same prompt for other models

Identical content, packaged the way each model reads most reliably.

ChatGPT

## Role

You are a senior software engineer who writes production-ready, tested code.

## Context

Language or tool: {{flavour}}. Examples that should match: {{should_match}}. Examples that should not: {{should_not_match}}.

## Task

Write a regular expression that matches {{what_to_match}}.

## Constraints

- Explain each part of the expression in order.
- Show it correctly matching every positive example and rejecting every negative one.
- Flag any catastrophic backtracking risk.
- Prefer a readable expression over a clever one.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

## Output format

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

## Approach

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

## Before you start

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, write a regular expression that matches {{what_to_match}}.

Gemini

**Task**

Write a regular expression that matches {{what_to_match}}.

**Role**

You are a senior software engineer who writes production-ready, tested code.

**Context**

Language or tool: {{flavour}}. Examples that should match: {{should_match}}. Examples that should not: {{should_not_match}}.

**Constraints**

- Explain each part of the expression in order.
- Show it correctly matching every positive example and rejecting every negative one.
- Flag any catastrophic backtracking risk.
- Prefer a readable expression over a clever one.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

**Output format**

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

**Approach**

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

**Before you start**

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, write a regular expression that matches {{what_to_match}}.

Common questions

Why does my AI-generated regex match things it should not?
Because the prompt only gave positive examples. A pattern that matches everything you listed can still match a great deal more, and negative examples are what constrain it.
What is catastrophic backtracking?
A pattern with nested quantifiers can take exponential time on input that nearly matches, freezing whatever runs it. It matters whenever a regex touches user-supplied input, and it is easy to introduce accidentally.
Which AI model is best for writing a regular expression?
All of them handle this; what changes is the packaging. This page shows the same prompt written for Claude, ChatGPT, Gemini. Claude follows XML-delimited structure most reliably, ChatGPT works best with markdown headings, and Gemini prefers the task stated before the material. The content of the prompt is identical in each.
Can I change this prompt for my own situation?
Yes, and you should. Replace the placeholders with your own details, then open it in the builder to add context specific to you. The builder scores what you supply and tells you exactly which missing piece would improve it most.
Why does this prompt include rules I did not ask for?
Because coding tasks carry requirements that experienced practitioners apply automatically and rarely write down. The compiler adds them so the output does not fail on something obvious. Every added rule is listed on the how it works page.

Browse all ready-made prompts, the full prompt library, or read the guides.