Advanced

Pattern tools

Patterns live in files and configs. The tools around them decide whether a rule stays readable after the first edit.

What you will see

3

About 2 min of reading

Try It

Check the rule tools.

1

Rules compiled.

import re RULES = ["^\d{4}-\d{2}-\d{2}$", "^[a-z]+$"] pats = [re.compile(r) for r in RULES] print(len(pats))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2
2

Pattern shown.

import re pat = re.compile("(?P\d{4})") print(pat.pattern)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
(?P\d{4})
3

Literal escaped.

import re lit = re.escape("a.b+c") print(bool(re.search(lit, "a.b+c")))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
True
4

Runs counted.

import re print(len(re.findall("\w+", "a bb ccc")))

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

Rules are stored as text, compiled once at start, then run against samples so a broken rule shows at once.
1 store the rule
↓
2 compile at start
↓
3 run the samples
↓
4 report the broken rule
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
RULES = ["^\d{4}-\d{2}-\d{2}$", "^[a-z]+$"] sets nothing on screen
pats = [re.compile(r) for r in RULES] sets nothing on screen
print(len(pats)) 2
Why this works
Rules are data

A rule in a list can be edited, listed and tested without touching code. A rule buried in an expression cannot.

Escape from input

Text that came from a person is not a pattern. Escaping it stops a dot in their input from becoming a wildcard.

Examples

Keep a rule file

import re
RULES = ["^\d{4}-\d{2}-\d{2}$", "^[a-z]+$"]
pats = [re.compile(r) for r in RULES]
print(len(pats))

Rules as a list of strings, compiled once. Editing a rule is a text edit.

Name every rule

import re
pat = re.compile("(?P<year>\d{4})")
print(pat.pattern)

The pattern stays next to the code that reads it. A name says what each field is.

Test a rule alone

import re
pat = re.compile("\d+")
print(bool(pat.search("12")))

Run one pattern against one string. A test per rule finds the broken one.

Debug a miss

import re
print(re.findall("\d+", "a1b22"))

Print the matches, not the boolean. A miss shows which part failed.

Escape a literal

import re
lit = re.escape("a.b+c")
print(bool(re.search(lit, "a.b+c")))

escape makes every special character literal. Hand escaping misses one.

A rule that is too wide

import re
print(len(re.findall("\w+", "a bb ccc")))

A wide class matches more than the rule meant. Narrow it before shipping.

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.

← Cost of a pattern
Previous Lesson
Cost of a pattern
Next Lesson
How the engine walks
How the engine walks →