A registration sheet contains four identifiers: EVT-2026-0042, EVT-2026-42, evt-2026-0042, and EVT-2026-0042-extra. Which can go into the check-in system? Under a rule requiring uppercase EVT, a four-digit year and a four-digit sequence, only the first qualifies. This small example is a useful way to practise AI-assisted regular expressions: the rule is explicit, the outcome is testable, and the method transfers to everyday data work.
This tutorial is for people handling forms, operations lists and internal tools. We will use Python's standard library for a runnable example, from a written requirement to individual validation results. No paid regex extension is required. AI proposes an expression; local code checks it. All identifiers below are fictional.
Write a rule that can be graded
The complete format is EVT-YYYY-NNNN. EVT must be uppercase, separators must be ordinary ASCII hyphens, and both numeric sections must contain exactly four ASCII digits. We check the year format without restricting its value. Leading zeroes and sequence 0000 are allowed. Leading or trailing spaces, newlines and appended notes are forbidden. Decide these details before generating code.
Distinguish validating a whole cell from finding an identifier inside a sentence. Validation requires the entire value to comply; extraction allows surrounding text. This example validates a complete cell. Extracting identifiers from email requires its own specification, rather than simply making this validator more permissive.
Use an existing text assistant approved for your work, such as Claude. Its role here is to explain and revise rules around examples; no exclusive feature is needed. A short synthetic sample avoids uploading the entire registration file or buying another tool. The actual execution environment is what matters.
Include expected answers in your prompt
Write a registration-ID validator using Python 3's standard re library. The format is EVT-YYYY-NNNN, with uppercase EVT and exactly four ASCII digits in each numeric section. Reject surrounding whitespace and extra characters. Allow sequence 0000 and do not restrict the year value. Match the whole string. Explain the rule, provide the pattern and a function returning True or False. Do not add input cleanup. Positive examples: EVT-2026-0042 and EVT-2026-0000. Negative examples: EVT-2026-42, evt-2026-0042, EVT-2026-0042-extra, and an otherwise valid ID with a leading space. Add boundary cases and explain what each tests.
Positive cases are a starting point; negative cases expose permissive patterns. A response that strips whitespace before checking has changed the assignment: malformed original data will pass. Cleanup can be a separate later operation, but this check must reveal defects in the original. Ask the assistant to explain every added operation.
Run the example: expect nine PASS lines
Save the code below as check_event_id.py and run python check_event_id.py with Python 3 installed; some systems use python3. It checks in-memory strings without accessing business files. PASS means the actual decision matches the expected decision. A correctly rejected invalid identifier also prints PASS.
import re
pattern = re.compile(r"EVT-[0-9]{4}-[0-9]{4}")
def valid(value):
return pattern.fullmatch(value) is not None
cases = [
("EVT-2026-0042", True),
("EVT-2026-0000", True),
("EVT-2026-42", False),
("evt-2026-0042", False),
("EVT-2026-0042-extra", False),
(" EVT-2026-0042", False),
("EVT-2026-0042\n", False),
("EVT-2026-0042", False),
("", False),
]
for value, expected in cases:
actual = valid(value)
print("PASS" if actual == expected else "FAIL", repr(value), actual)
assert actual == expected
fullmatch requires the entire string to match, and [0-9] explicitly limits the digits to ASCII. These behaviours are documented in the official Python re reference. The trailing-newline and full-width-digit negative cases catch differences that are easy to miss visually.
Give AI a precise failure to fix
If another implementation accepts a lowercase prefix, supply its pattern, failing input, actual result and expected result. Request a minimal correction instead of saying only that it is wrong. That helps prevent a casing fix from also relaxing sequence length. Rerun all cases after each change and keep existing negative examples.
Here is the current code. Input evt-2026-0042 returns True but should return False. Identify the exact statement causing the discrepancy, provide the smallest change and explain its effect on the other eight cases. Preserve the original requirement. Do not trim whitespace or allow variable-length sequences. For any additional tests, give input, expected result and rationale.
If the business instead decides to accept three-digit sequences, revise the written rule and expected tests before changing the expression. Record requirement changes separately from defects, so a colleague knows why a value is accepted next month.
When processing a list, report invalid rows first
Call valid for each row in a working copy, retaining the original row number, identifier and result. Export failures to an exception list. Read CSV with a CSV parser rather than splitting an entire line with the identifier regex: quoted commas change field boundaries. Preserve identifiers as strings to retain leading zeroes.
Start with a few dozen real rows and inspect examples from both accepted and rejected groups. Empty cells, non-string values and missing columns deserve separate input-error handling. This function assumes a string; it does not define every type error as an invalid identifier. Agree how to handle missing values before calling it.
Format validation cannot determine whether an ID is duplicated, the event exists or a person has already checked in. Perform those business checks separately: validate format, check full-ID duplicates, then verify the record in the approved system. Passing this pattern satisfies just one condition.
Keep three things for reuse
- A precise written specification covering case, whitespace, lengths and allowed values.
- Positive, negative and boundary examples with expected outcomes.
- The code that passed locally, the Python version and the reason for the change.
For JavaScript, spreadsheet software or another platform, restate the target environment and rerun the same cases. Similar-looking expressions can have different matching functions and escaping rules. The habit developed here also applies to filenames, SKUs and internal ticket numbers: turn requirements into checkable inputs and outputs so AI suggestions have a clear acceptance test.