Regular expressions with AI: how to get a working regex and test it

A model writes regex fast but errs confidently on edge cases. A request with examples, an auto-check loop and protection against hangs.

5 min · · development · автоматизация

AI for regular expressions saves you the most unpleasant part: remembering syntax and picking escapes. But an expression that looks right often breaks on a string you did not show. A reliable way to work: describe the task with examples, get a regex with an explanation, run it on test strings in code and only then use it. Below are the request, a script with an auto-check and a list of traps. The script is in Python, but the approach is the same for any language.

How to describe the task

A verbal description ("find phone numbers") leaves too much freedom. Give the model four things:

  1. The dialect. Python re, JavaScript, PCRE, Go RE2: they differ (support for lookbehind, named groups, unicode). Without a dialect you get syntax at random.
  2. What must match. A list of strings that have to fit.
  3. What must not. A list of strings that have to be rejected. Negative examples are what separate a working expression from an overly greedy one.
  4. The mode. Whether you check a whole string or search inside text; whether you need capture groups.

An example request:

bash
Dialect: Python re. Mode: check the whole string.
I need a regex for Russian phone numbers in the formats +7 (912) 345-67-89,
8 912 345 67 89, +79123456789, 8(912)3456789.
Must match: [list]. Must not match: [list].
Return JSON with the fields pattern, explanation (part by part) and risks
(what may fire unexpectedly). JSON only.

The "part by part" requirement matters: the explanation lets you read the expression and understand it instead of trusting it blindly.

Auto-check in code

The model does not run the expression, so we check it ourselves. The script asks the model for a regex, runs it on positive and negative examples and on failures returns the list of failures to the model.

bash
import json
import re
from openai import OpenAI

client = OpenAI(base_url="https://api.zukko.pro/v1", api_key="fetch_your_key")

POSITIVE = ["+7 (912) 345-67-89", "8 912 345 67 89", "+79123456789", "8(912)3456789"]
NEGATIVE = ["+7 (912) 345-67", "912 345 67 89", "+1 912 345 67 89", "12345678901234"]

TASK = (
    "Dialect: Python re, whole-string check (fullmatch will be used). "
    "I need a regex for Russian phone numbers. "
    f"Must match: {POSITIVE}. Must not match: {NEGATIVE}. "
    'Return only JSON: {"pattern": "...", "explanation": "..."}.'

The principle: the model proposes, code checks, errors go back into the dialogue. If the expression has not passed the check in three attempts, the task is most likely underspecified: add examples. The example is in Python, but the same principle works with JavaScript or another language: only the check function changes.

Traps the tests will not catch

Catastrophic backtracking (ReDoS). Expressions with nested repetitions like (a+)+$ take exponentially long on a suitable string and can hang a service. If the regex is applied to user input, test it on a long "almost matching" string and cap the input length, or use an engine without backtracking (RE2). Ask the model to assess this separately in the risks field, but do not rely on its assessment alone.

bash
import re, time

rx = re.compile(r"^(\d+)+$")
bad = "1" * 22 + "x"
t = time.perf_counter()
rx.match(bad)
print(f"{time.perf_counter() - t:.2f} s")

If the time grows as you add characters to bad, the expression is dangerous.

Unicode. \w and \d treat Cyrillic and non-Latin digits differently in different dialects. If you have Russian text, add such strings to the examples.

Boundaries. Whether you need ^ and $, \b, multiline mode. State the mode in the request: whole-string check or search inside text.

"Real" validation. E-mails, URLs, dates and tax IDs cannot be fully validated with a regular expression. A ready-made library or a checksum is better for that. Regex suits format, not meaning.

Readability and maintenance

Ask the model to write the expression in verbose mode with comments (re.VERBOSE in Python) and keep the explanation next to it in the code. In six months you yourself will not remember what the (?:\+7|8) group does. A good habit is to keep in the repository a file with the test strings you used to check the expression. They are the same examples and will be useful on any change. If you specifically need a test set, ask the model to generate additional edge-case examples, then check by hand that their labels (matches or not) are right: a model sometimes errs precisely in the label.

What it costs

One request: roughly 1,000 input tokens (task, examples) and 500 output. Formula: input × input price + output × output price. Prices as of October 18, 2026, $ per 1M tokens:

  • claude-sonnet-5-5 (0.4 / 2): 0.001 × 0.4 + 0.0005 × 2 = 0.0004 + 0.001 = $0.0014.
  • gpt-6.1-sol (0.27 / 1.32): 0.001 × 0.27 + 0.0005 × 1.32 = 0.00027 + 0.00066 = $0.00093.
  • glm-5.3-flash (0.04 / 0.04): 0.001 × 0.04 + 0.0005 × 0.04 = 0.00004 + 0.00002 = $0.00006.

With three attempts, where the history accumulates in each next one, the sum grows to a few tenths of a cent on Sonnet. For this kind of task cost barely affects the choice: what matters more is how the model copes with your dialect. Try a cheap one, and if it fails the check, switch to a stronger one. Model strength by the Artificial Analysis index in the catalog: GLM 5.3 Flash is 42, GPT-6.1 Sol is 52, Sonnet 5.5 is 48; that is no guarantee, so test on your own examples. Prices change; the current ones come from GET https://api.zukko.pro/v1/models.

Full endpoint description: Chat Completions. If you want the model to answer strictly in JSON, read about structured output. For code generation in general, see the article on prompts for programmers.

To run the script, create a key in the dashboard and top up the balance: SBP, a MasterCard card or cryptocurrency.

FAQ

Can I trust a regex from AI without checking?

No. The expression looks convincing but may fail on edge cases. Always run it on positive and negative examples and check for ReDoS.

Which AI is best for generating code and regexes?

We cannot say in advance. With regex, the check in code matters more than the choice of model. Compare two or three models on your own examples.

How do I get a regex for another language?

State the dialect in the request and test in that language. Named groups, lookbehind and unicode differ between JavaScript, Go and Python.

What if the expression turns out too complex?

Split the task: a rough match with a regular expression first, then a check in code. Part of the logic is simpler and more reliable to express with ordinary conditions.

zukko

Pay 25×
less

for Claude Code & ChatGPT

One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.

Get API keyDocumentation

api.zukko.pro · fetch_*

AI for regular expressions: a regex and how to test it · Zukko