Basic

What a pattern is

A pattern is a small language for describing text. One pattern can match many different strings, which is why it replaces long chains of checks.

What you will see

cat

About 2 min of reading

Try It

Check what a pattern matches.

1

Match found.

import re print(bool(re.search("cat", "the cat sat")))

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

What it prints
True
2

Dot matches.

import re print(bool(re.search("c.t", "cot")))

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

What it prints
True
3

No match.

import re print(re.search("dog", "the cat sat") is None)

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

What it prints
True
4

Text matched.

import re hit = re.search("c.t", "the cat sat") print(hit.group())

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

What it prints
cat

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 pattern is compiled into small steps, run against the text from left to right, and reports the first place it fits.
1 write the pattern
↓
2 compile the steps
↓
3 scan the text
↓
4 report the match
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
print(bool(re.search("cat", "the cat sat"))) True
Why this works
A pattern is not a string

A plain pattern looks like text, but characters like the dot and the star carry meaning. That is why a literal dot must be escaped.

Search is a yes or no

A search answers whether a match exists and where. It says nothing about the rest of the text until you ask again.

Examples

Match a plain word

import re
print(bool(re.search("cat", "the cat sat")))

Every character in a plain pattern means itself. The search looks anywhere in the text.

A dot is any character

import re
print(bool(re.search("c.t", "cat")))

The dot stands for one character of anything. It matches cat, cot and cut.

Search finds anywhere

import re
print(bool(re.search("cat", "category")))

search finds a match inside a longer word. That is often too loose for a rule.

Match returns the text

import re
hit = re.search("c.t", "the cat sat")
print(hit.group())

The match object holds the exact text that matched, plus where it started.

No match is none

import re
hit = re.search("dog", "the cat sat")
print(hit is None)

No match gives None, not an empty string. Check before using the result.

Patterns are strings

import re
pat = "c.t"
print(bool(re.search(pat, "cot")))

A pattern is text you can build and store. Keep it in a variable when it is reused.

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.

Next Lesson
Character classes
Character classes →