Regular Expressions

Scenario: Thousands of log lines contain structured request IDs that plain splitting cannot safely isolate.

New words, in plain English

The re module offers search, match, fullmatch, findall, finditer, sub, and compiled patterns. Use named groups and anchors when structure matters. Regex is powerful but ordinary string methods are clearer for simple text.

Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.

A small, real example

import re
pattern = re.compile(r"request=(?P<id>[A-F0-9]{8})")
match = pattern.search("INFO request=DEADBEEF ok")
print(match.group("id"))
Tip: Use fullmatch for validation so extra unwanted text cannot sneak past.
Goal: Practice this idea in the py-regex-datetime challenge at /labs/python. Fix the broken program, run it yourself, then use check to prove the real end state.