Pitfalls

Catastrophic backtracking on a tiny string; re.error.

A nested quantifier on a tiny string can backtrack a lot. Also re.error if the pattern is broken. Keep demos small.

Goal

Time a bad pattern on a short string, then catch a bad pattern.

import re
from time import perf_counter
pat = re.compile(r"(a+)+b")
start = perf_counter()
print(pat.search("a" * 18 + "c"))
print("ms", round((perf_counter() - start) * 1000, 1))
import re
try:
    re.compile(r"(")
except re.error as err:
    print(type(err).__name__, err)
import re
# A raw string keeps \d as a class, not a backspace-ish escape surprise in some contexts.
print(re.findall(r"\d+", "12"))
print(re.findall("\\d+", "12"))
import re
print(re.search(r"Nairobi", "nairobi"))
print(re.search(r"Nairobi", "nairobi", re.I))
Pitfall

Do not run (a+)+ on long strings. That is the lesson — not a challenge.