How to Prompt AI to Write a Regular Expression

AI is great at regex if you prompt it with real examples and make it show test cases. Here is a template and how to verify the pattern before you ship it.

Steve Jefferson
Steve Jefferson
Developer Advocate
24 August 20261 min read

How to Prompt AI to Write a Regular Expression

The fastest way to prompt AI to write a regular expression is to stop describing the pattern in words and start showing it examples. Give the model three or four strings that should match, three or four that should not, and ask for the regex plus a plain-English breakdown and its own test cases. Regex is dense and easy to get subtly wrong, so the examples pin down what you actually mean, and the test cases catch the model when it drifts. Description-only prompts are where the bad patterns come from.

This is a specific case of good prompt engineering, and it works the same way as getting AI to write SQL queries: concrete inputs beat abstract instructions.

A prompt template for a regular expression

Copy this, fill in your examples, and you will get a usable pattern far more often than with a one-line request:

Write a regular expression for [FLAVOUR, e.g. JavaScript / PCRE / Python re].

Should MATCH:
  - example-that-matches-1
  - example-that-matches-2
  - example-that-matches-3

Should NOT match:
  - example-that-fails-1
  - example-that-fails-2

Requirements: [e.g. case-insensitive, no capturing of the domain].
Return:
  1. the regex
  2. a plain-English explanation of each part
  3. a table testing it against every example above, match or no match

Naming the flavour matters more than people expect. A pattern that works in one engine can break in another because of lookbehind support, escaping, or Unicode handling. Tell the model whether you are in JavaScript, Python, PCRE, or a specific tool, and you avoid a whole class of subtle failures.

A bad prompt and a good one

A vague prompt: write a regex for an email. You will get one of the famous over-complicated email patterns that either rejects valid addresses or accepts nonsense, and you will not know which. A good prompt gives the model the exact addresses you care about:

Write a JavaScript regex for internal emails.
Should MATCH:   jo@acme.com   sam.lee@acme.com   a_b@acme.com
Should NOT match: jo@gmail.com   jo@acme.co   @acme.com   jo@acme
Return the regex, an explanation, and a test table.

Now the model has a target. Internal domain only, allow dots and underscores in the local part, reject other domains. The output is checkable against the examples you gave, which is the whole point.

Common patterns to ask for by example

Most day-to-day regex needs fall into a handful of shapes. In every case, lead with examples rather than a description:

You want

Give it these examples

Watch out for

A field like a phone or ID

5 real formats that vary

Optional separators and country prefixes

Extracting a value from text

The surrounding text, not just the value

Greedy matching grabbing too much

Validating a format

Valid and near-miss invalid strings

Edge cases like empty or trailing spaces

Splitting or replacing

Before and after strings

Special characters that need escaping

Verify before you trust it

This is the step people skip and regret. A regex that passes the model's own examples can still be wrong on the real data, so verify it yourself before it goes near production:

  1. Paste the pattern into a live tester like regex101 and run it against real samples, including the ugly ones from your actual data.

  2. Try to break it. Feed it empty strings, very long inputs, unusual characters, and near-misses. Regex failures hide in the inputs you did not think of.

  3. Read the explanation against a reference. The MDN regular expressions guide is the clearest one for the common syntax, so you can confirm each part does what the model claimed.

  4. Watch for catastrophic backtracking. A pattern with nested quantifiers can hang on certain inputs, which is a denial-of-service risk if it runs on user data.

If the pattern misbehaves, the same principles that help you debug AI-generated code apply: narrow it to the smallest failing input and ask the model to fix that specific case, with the failing string included.

Frequently asked questions

Why does AI get regex wrong so often?

Because regex packs a lot of meaning into a few characters, and a single wrong quantifier or missing escape changes everything. The model is pattern-matching from training data, so an unusual requirement described only in words is easy to miss. Examples and test cases are what make it reliable.

Should I ask for the simplest regex or the most correct one?

Ask for the simplest pattern that passes all your examples. Overly clever regex is hard to read and maintain, and a slightly longer, clearer pattern is almost always the better choice for real code. Tell the model to prefer readability.

Can AI explain a regex I already have?

Yes, and it is one of the best uses. Paste an inherited pattern and ask for a line-by-line breakdown and some example inputs it matches. That is often faster than decoding it yourself, and it helps you spot whether the pattern actually does what the previous author intended.

Is it safe to use AI regex on user input?

Only after you check for catastrophic backtracking and test it on hostile inputs. A poorly formed pattern can be exploited to hang your server. Verify performance on long and adversarial strings before any regex touches untrusted input, whether a human or an AI wrote it.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.