Regex with Match and Non-Match Test Strings

Writes a regex for your pattern, explains each part and provides 8 strings that should match and 8 that should not.

You get: Working code with an explanation

More like this

The prompt

regex-with-match-and-non-match-test-strings.md
You are a regex specialist who writes patterns that are correct and maintainable. I need a regex that matches this: {describe the pattern to match}. If the regex flavor or language (JavaScript, Python, PCRE, Java, Go) or the exact boundaries are unclear, ask me up to two short questions first. Rules: anchor the pattern when the whole string must match; avoid catastrophic backtracking; make the non-matching strings realistic near-misses, not obvious junk; state clearly anything the regex deliberately does not validate. Output format: 1) Assumptions. 2) The regex in a code block, with flags noted. 3) A table explaining each part of the pattern in plain words. 4) Eight strings that should match, each with a short reason. 5) Eight strings that should not match, each with the reason it fails. 6) Limitations and cases better handled by a parser or validator. 7) A short test snippet in my language I can run. The answer is good if all sixteen strings behave as labeled when I run them.

Example output

AI-written example
import re

# ASSUMPTIONS
# - Flavor: Python 3 (re module). Whole string must match; no surrounding text.
# - Format: ISO 8601 calendar date YYYY-MM-DD, zero-padded, ASCII digits only.
# - Flags: none needed. [0-9] is used instead of \d so Unicode digits don't match.
#   \A and \Z are used instead of ^ and $ so a trailing newline is rejected.

DATE_RE = re.compile(
    r"""
    \A
    [0-9]{4}                         # year: exactly four digits
    -                                # literal hyphen
    (?:0[1-9]|1[0-2])                # month: 01-09 or 10-12
    -                                # literal hyphen
    (?:0[1-9]|[12][0-9]|3[01])       # day: 01-09, 10-29, or 30-31
    \Z
    """,
    re.VERBOSE,
)
# No nested quantifiers or overlapping alternations, so no catastrophic backtracking.

SHOULD_MATCH = [
    ("2024-01-01", "typical date, first day of year"),
    ("1999-12-31", "upper month and day boundary"),
    ("2000-02-29", "leap day is accepted (day range only)"),
    ("2025-10-10", "month starting with 1, day starting with 1"),
    ("0001-01-01", "leading zeros in year are allowed"),
    ("2030-06-15", "mid-month day in 10-29 range"),
    ("2024-09-30", "month 09 and day 30"),
    ("9999-11-20", "maximum four-digit year"),
]

SHOULD_NOT_MATCH = [
    ("2024-13-01", "month 13 is out of range"),
    ("2024-00-10", "month 00 is invalid"),
    ("2024-04-32", "day 32 is out of range"),
    ("2024-04-00", "day 00 is invalid"),
    ("2024-4-5", "month and day are not zero-padded"),
    ("2024/04/05", "slashes instead of hyphens"),
    (" 2
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Use when you need a pattern and want proof it works. Describe what should match in plain words, with a couple of real examples if you have them.

Questions about this prompt

More in Coding

Architecture Decision Record for a Technology Choice

Compares realistic options against your constraints and produces a decision record. The record has a weighted comparison, risks, a reversal plan, and a recommendation the team can approve or challenge.

Explain This Error with Ranked Causes

Explains an error and stack trace in plain words, ranks the three most likely causes and gives a confirming check for each.

SQL Query from Schema and Question

Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

Business Idea Validation Sprint with a $100 Two-Week Test

Turn a business idea and target customer into a ranked list of risky assumptions, five interview questions and a $100 test to run in two weeks.

Positioning and Messaging Hierarchy Builder from Competitor and Customer Evidence

Turns your product facts, customer evidence, and competitor notes into a tested positioning statement, messaging hierarchy, and proof-gap list so a marketing team can decide what to say and what to validate first.

20-Case Agent Eval Set with Pass/Fail Rubric for Normal, Edge and Unsafe Requests

Builds 20 test cases for your agent's task, with expected behavior, failure signals and a pass/fail rubric covering normal, edge and unsafe requests.

Strict JSON Handoff Schemas with Validation and Retry Rules for Agent Pipelines

Turns a pasted pipeline description into JSON schemas for every agent-to-agent handoff, plus validation checks and retry rules for bad output.

Pull Request Review with Merge Recommendation

Reviews a diff against your standards and returns a merge, merge-with-changes, or do-not-merge recommendation. Findings are ranked by severity, with evidence, suggested fixes, and open questions.

Regex Builder With Match and No-Match Test Cases

Builds a regular expression from a plain-English description and example strings, explains each part, and proves it against strings that should and should not match.