Advanced

Replacing with a pattern

A pattern finds text and a replacement puts text back. That pair does most edits that a loop of string slicing would otherwise need.

What you will see

3

About 2 min of reading

Try It

Check what a swap produces.

1

Words swapped.

import re out = re.sub("(\w+) (\w+)", "\2 \1", "first last") print(out)

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

What it prints
last first
2

Spaces collapsed.

import re print(re.sub("\s+", " ", "a b c"))

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

What it prints
a b c
3

Swaps counted.

import re out, n = re.subn("x", "y", "xyx") print(n)

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

What it prints
2
4

Parts split.

import re print(len(re.split("[,;]", "a,b;c")))

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

Each match is found, the replacement is built from it, and the text before and after is copied unchanged.
1 find a match
↓
2 build the replacement
↓
3 copy the gaps
↓
4 report the count
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
out = re.sub("(\w+) (\w+)", "\2 \1", "first last") sets nothing on screen
print(out) last first
Why this works
The replacement is text

Group references are expanded in the replacement, but a backslash there means something else than in the pattern. Keep both sides separate.

A zero swap is a signal

A sub that changes nothing is a pattern that missed. Check the count when the edit matters.

Examples

Swap the words

import re
out = re.sub("(\w+) (\w+)", "\2 \1", "first last")
print(out)

The replacement can refer to captured groups by number.

Fix a format

import re
out = re.sub("\s+", " ", "a  b   c")
print(out)

Runs of whitespace collapse to one space in a single call.

Count the swaps

import re
out, n = re.subn("x", "y", "xyx")
print(n)

subn reports how many swaps happened. A zero count means the pattern missed.

Cap the swaps

import re
out = re.sub("x", "y", "xxxx", count=2)
print(out)

A count limit edits the front only. Use it when the rest must stay.

A function swap

import re
out = re.sub("\d", lambda m: m.group() + "!", "12")
print(out)

A function gets each match, so the swap can depend on what matched.

Split on a pattern

import re
parts = re.split("[,;]", "a,b;c")
print(len(parts))

Split takes a pattern, so several separators go in one call.

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.

← Greedy and lazy
Previous Lesson
Greedy and lazy
Next Lesson
Pulling fields out
Pulling fields out →