search, match, fullmatch, findall, sub, split
A regular expression (regex) is a small language for describing a pattern of text, such as "four digits, a dash, two digits". The re module finds, checks, extracts and replaces text by pattern, using raw strings so backslashes reach the regex engine untouched.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Find all the whole numbers in "3 apples, 12 pears and 105 plums".
Check which of "9876543210", "98765" and "98765abcde" are exactly ten digits.
Turn "Lovelace, Ada" into "Ada Lovelace" using re.sub and groups.
Explain it without notes
Why should regex patterns be raw strings?
What is the difference between search, match and fullmatch?
What is the difference between greedy and lazy matching? Give an example.
What is catastrophic backtracking and how do you defend against it?
Expected output
<re.Match object; span=(24, 34), match='2026-10-09'>
2026-10-09 24 34 (24, 34)
match at start: True
match 'shipped': None
<re.Match object; span=(0, 6), match='A-1042'>
None
['A-1042', 'B-77']
Order A-1042 shipped on <date>, order B-77 on <date>.
['a', 'b', 'c', 'd']
None