Ship a Regex With Its Test Cases

AI-assisted article. Reviewed by Codebuff automated editorial review on 2026-11-13. Examples are illustrative, not real user data.
A regular expression is the densest thing most people will ever paste into a chat window. Twenty characters can encode a month of edge cases, and the reader has no way to tell whether the pattern is careful or lucky. The fix is not a longer explanation; it is a short table of inputs and expected results that the pattern either satisfies or does not. With the cases attached, a reviewer can argue about behaviour instead of reading syntax.
Why can't a pattern be reviewed on its own?
Looking at an expression tells you what it appears to do. Running it against a handful of strings tells you what it does. Reviewers who are handed only the pattern either trust it or rewrite it from scratch, and both outcomes waste the work that went into the edge cases.
Attaching three or four inputs takes a minute and turns a syntax argument into a behaviour question, which is the question that actually matters when the pattern is deciding what counts as a valid identifier or a log line.
Which inputs must match?
Start with the ordinary input the pattern exists for, then add the awkward variant that made you write it in the first place: the leading space, the empty suffix, the unicode dash. Those are the cases that distinguish a working expression from one that happens to pass on the sample.
Keep each case on its own line with its expected result, so the list can be turned into a test without rewriting it.
# pattern: ^[a-z0-9]+(?:-[a-z0-9]+)*$
# intended: a slug, lowercase, hyphen-separated, no leading or trailing hyphen
match release-notes
match a
match v2-0-1
no match Release-Notes # uppercase
no match -leading # leading hyphen
no match trailing- # trailing hyphen
no match two--hyphens # empty segment
no match "" # empty string
Which inputs must not match?
The negative cases carry most of the information, because a permissive pattern passes every positive test. If someone hands you a pattern and four examples that all match, the interesting question is still unanswered.
Include the near miss: the string that looks like it should match and does not, or the one that matches by accident. That single line often reveals whether the anchors are doing what the author thinks.
Which regex engine does the pattern target?
Lookbehind, possessive quantifiers, named groups, unicode property escapes and the meaning of dot all differ between JavaScript, Python, PCRE, Go and POSIX tools. A pattern that works in one engine can be a syntax error or, worse, a silent no-op in another.
Quote the flags as well. The same expression with and without the multiline flag matches different text, and a reader who cannot see the flags will reproduce your result only by accident.
Sources and editorial review
Prepared with AI assistance and checked against the code-sharing and text-comparison pages at the reviewed revision: the pattern-plus-cases framing, the negative and near-miss cases, and the engine/flags caveat. The example pattern is illustrative and was not benchmarked against a specific engine's dialect.