Master

Building a pattern

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.
1 write a part
↓
2 test each part
↓
3 join into a rule
↓
4 compile 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.

import re sets nothing on screen
DATE = "\d{4}-\d{2}-\d{2}" sets nothing on screen
TIME = "\d{2}:\d{2}" sets nothing on screen
rule = DATE + " " + TIME sets nothing on screen
print(bool(re.fullmatch(rule, "2026-10-12 07:30"))) True
Why this works
Assemble, do not write

A long pattern written once cannot be read. The same rule built from named parts reads like a list of fields.

Print what you built

The assembled string is the truth. Printing it catches a missing join or a doubled bracket that no test names.

Examples

Parts into a 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")))

Named parts join into a rule. Each part is tested on its own.

Wrap a part

import re
FIELD = "(?P<name>\w+)"
rule = FIELD + " " + FIELD
hit = re.fullmatch(rule, "one two")
print(len(hit.groupdict()))

A part carries its own name, so the assembled rule keeps both fields.

Repeat a part

import re
ITEM = "\w+,"
rule = ITEM * 3
print(bool(re.fullmatch(rule, "a,b,c,")))

A part repeated in text is a repetition the engine then walks.

Optional part

import re
UNIT = "( (?P<unit>\w+))?"
rule = "\d+" + UNIT
hit = re.fullmatch(rule, "12")
print(hit.group("unit"))

An optional part gives None when it is absent, which the caller must handle.

Show the rule

import re
rule = "\d+" + "-\d+"
pat = re.compile(rule)
print(pat.pattern)

Print the assembled pattern. A missing join character shows up here.

Test the parts

import re
parts = ["\d{2}", "[a-z]{3}"]
ok = sum(1 for p in parts if re.fullmatch(p, "12abc"))
print(ok)

Each part gets its own sample. A broken part is found before assembly.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Patterns and strangers
Previous Lesson
Patterns and strangers