Rules get assembled from parts. Building them keeps each piece testable, which is the only way a long pattern stays fixable.
What you will see
2
About 2 min of reading
Quick check
Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.
Try It
Check what an assembly produces.
1
Joined rule.
import re
DATE = "\d{4}-\d{2}-\d{2}"
TIME = "\d{2}:\d{2}"
rule = DATE + " " + TIME
print(bool(re.fullmatch(rule, "2026-10-12 07:30")))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
True
2
Fields kept.
import re
FIELD = "(?P\w+)"
rule = FIELD + " " + FIELD
hit = re.fullmatch(rule, "one two")
print(len(hit.groupdict()))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
2
3
Repeated part.
import re
ITEM = "\w+,"
print(bool(re.fullmatch(ITEM * 3, "a,b,c,")))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
True
4
Parts passing.
import re
parts = ["\d{2}", "[a-z]{3}"]
ok = sum(1 for p in parts if re.fullmatch(p, "12abc"))
print(ok)
Before you run it, say out loud what it will print. Then open the answer.
What it prints
2
Put it in order
Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.
Data Flow
Named parts are tested alone, joined into a rule, printed for reading, then compiled once for use.
1write a part
↓
2test each part
↓
3join into a rule
↓
4compile and print
Line by line
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.