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
- Write the spec in one sentence that names the exact input shape, for example "a four-digit year with no sign and no spaces."
- 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.
- 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.
- Run the table in Python. Use
fullmatchfor 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))- 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 withfullmatchfor whole-string rules. - `$` and the trailing newline. In Python,
foo$matches"foo\n"by default. Add a"foo\n"row to the table. Usefullmatch, or test the newline case explicitly. - Unicode digits. In a Python
strpattern,\dalso matches digits such as Arabic-Indic ones. A row with such digits in the must-not-match list exposes this. Usere.ASCIIwhen 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
gflag,exec()andlastIndexcarry state between calls. If the table loop reuses one object, results alternate between runs. Create the regex fresh for each test, or resetlastIndexto 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.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
- How to Use an AI Agent on a First Task You Can Check
- No-code AI agent builders: what to check first
- How to Use AI to Write a Letter: Facts First, Then Tone
- Remotion Agent Skills: Install and Make a First Video
- How to Write a PowerShell Script with Safe Dry Runs
- How to write a bug report a developer can reproduce
Sources
- https://docs.python.org/3/library/re.html (accessed 2026-11-21)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions (accessed 2026-11-21)
This page is not offered to search engines yet: The content file does not mark this page indexable yet.