A validation pattern must accept every good value and reject every bad one. Both directions fail quietly, so the rule is written with cases.
What you will see
3
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 a rule accepts.
1
Good value.
import re
print(bool(re.fullmatch("\d{3}-\d{4}", "415-0192")))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
True
2
Too long.
import re
print(bool(re.fullmatch("\d{3}-\d{4}", "4156-0192")))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
False
3
Empty value.
import re
print(bool(re.fullmatch("\d+", "")))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
False
4
Rejects counted.
import re
cases = ["", "x", "415-01", "415-0192"]
bad = [c for c in cases if not re.fullmatch("\d{3}-\d{4}", c)]
print(len(bad))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
3
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
A validation runs the pattern against the whole value, then the answer is whether the fit covered all of it.
1take the value
↓
2run the pinned pattern
↓
3check full coverage
↓
4accept or reject
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.