Master

How the engine walks

A pattern is a small program. Knowing the order it tries things in turns guessing into reading, and reading is what fixes a wrong match.

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 the engine reports.

1

First start.

import re hit = re.search("a.c", "xxa c") print(hit.start())

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

What it prints
2
2

Branch chosen.

import re hit = re.search("cat|category", "category") print(hit.group())

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

What it prints
cat
3

Range shown.

import re hit = re.search("b", "abc") print(hit.end())

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

What it prints
3
4

Group offset.

import re hit = re.search("(a)(c)", "abc") print(hit.start(2))

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

The engine picks a start position, walks the pattern, and on failure moves the start forward one character.
1 pick a start
↓
2 walk the pattern
↓
3 fail at this start
↓
4 move the start forward
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
hit = re.search("a.c", "xxa c") sets nothing on screen
print(hit.start()) 2
Why this works
First fit wins

The engine stops at the first start position that works, not the longest match. Ordering branches is how you control that.

Ranges are half open

A match end is one past the last character. Slicing with both numbers gives exactly the matched text.

Examples

Left to right

import re
hit = re.search("a.c", "xxa c")
print(hit.start())

The scan starts at position zero and moves right. The first place that fits wins.

Alternation order

import re
hit = re.search("cat|category", "category")
print(hit.group())

Branches are tried in written order, so the shorter one wins here.

Longest first by hand

import re
hit = re.search("category|cat", "category")
print(hit.group())

Put the longer branch first when the whole word matters.

Anchored retry

import re
print(re.search("\d+", "ab 12").start())

A failure at one start position moves the start forward one character.

Positions are indexes

import re
hit = re.search("b", "abc")
print(hit.start(), hit.end())

A match reports the half open range it covered, which is how slicing works.

Groups are offsets

import re
hit = re.search("(a)(c)", "abc")
print(hit.start(2))

Each group carries its own start, so a field position is available.

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.

← Pattern tools
Previous Lesson
Pattern tools
Next Lesson
Patterns and strangers
Patterns and strangers →