How to Write a Regex with an AI Agent, Test Cases First

Write a regex from a spec by listing must-match and must-not-match strings first, then testing the pattern. Steps and pitfalls.

Updated · Markdown version

Write the test table before the pattern: list strings that must match and strings that must not match, then write the regex and run it against every row. This order gives an agent a fixed target, so each failing row points to one specific mistake instead of a general sense that the pattern is off.

Steps

  1. Write the spec in one sentence that names the exact input shape, for example "a four-digit year with no sign and no spaces."
  2. Build the table before asking for a pattern. Include at least five must-match rows and five must-not-match rows. Put the edge cases in the table: an empty string, a string with a trailing newline, a number with extra digits on either side, and digits from non-Latin scripts.
  3. Give the agent the table and ask for a pattern that satisfies every row. Keep the table in a file so the agent can rerun it after each change.
  4. Run the table in Python. Use fullmatch for whole-string rules:
import re

pattern = re.compile(r"\d{4}")
must_match = ["1999", "2026"]
must_not_match = ["999", "20260", "12a4", "1999\n"]

for s in must_match:
    if not pattern.fullmatch(s):
        print("MISS (should match):", repr(s))
for s in must_not_match:
    if pattern.fullmatch(s):
        print("HIT (should not match):", repr(s))
  1. Read the output. No lines printed means every row behaved as the table says. Then add one row you know should fail and confirm the script reports it, so you know the check can fail at all.

Pitfalls

  • `search` instead of `fullmatch`. re.search(r"\d{4}", "x1999y") returns a match, so a must-not-match row passes when it should fail. Notice it by adding rows with extra characters on either side. Fix it with fullmatch for whole-string rules.
  • `$` and the trailing newline. In Python, foo$ matches "foo\n" by default. Add a "foo\n" row to the table. Use fullmatch, or test the newline case explicitly.
  • Unicode digits. In a Python str pattern, \d also matches digits such as Arabic-Indic ones. A row with such digits in the must-not-match list exposes this. Use re.ASCII when only 0-9 should count.
  • Greedy versus lazy. <.*> on "<a> b <c>" matches the whole string, while <.*?> matches "<a>". Add a row with two tags in one string to catch this.
  • Shared regex objects in JavaScript. With the g flag, exec() and lastIndex carry state between calls. If the table loop reuses one object, results alternate between runs. Create the regex fresh for each test, or reset lastIndex to 0.

When to use a skill instead

Writing the table first is a fixed procedure, and it is easy to skip when a prompt is one request among many. The regex-case-tester skill takes a spec, builds the must-match and must-not-match table before it writes any pattern, runs that table with Python re, and reports the failing rows. It also flags patterns that risk catastrophic backtracking, which the table alone does not catch. Use the skill when you need the same check on many specs, and do the steps above by hand when you are testing one short pattern.

FAQ

How does a regex work?

A regex is a pattern that a matching engine compares against text. Python's re.search returns the first place the pattern matches, while re.fullmatch requires the whole string to fit the pattern.

What regex pattern can I use to match a 4-digit number?

Use \d{4} to find four digits inside a string, and call fullmatch with the same pattern to accept only strings that are exactly four digits. In Python, add re.ASCII if non-ASCII digits should not count.

What will the '$' regular expression match?

In Python, $ matches at the end of the string or just before a single newline at the end, so foo$ matches "foo\n". In JavaScript without the m flag, $ matches only at the end of the input.

Platform Score is our assessment under a published method. It is not a safety guarantee.

Next steps

  1. How to Use an AI Agent on a First Task You Can Check Read next.
  2. No-code AI agent builders: what to check first Read next.
  3. All guides by topic Browse everything.

Sources

This page is not offered to search engines yet: The content file does not mark this page indexable yet.