Python/Text & files 9 min
Regular expressions
Match, extract and replace patterns in text.
reregexpattern
The re module compiles a pattern and applies it to text. search finds the first match, findall returns every match, sub replaces them. Write patterns as raw strings (r"...") so backslashes survive.
python · editable
loading editor…
Try it: Extract only the order numbers that are followed by 'shipped'.
Groups in parentheses capture parts of a match, and named groups make the result readable.
python · editable
loading editor…
Building blocks worth memorising: \d digit, \w word character, \s whitespace, + one or more, * zero or more, ? optional, ^ start, $ end, [] a set, | alternatives.
practice this