Regex is a parser when you let it
The combination of capturing groups + anchors + character classes turns regex into a real parser for line-shaped data. Log lines, CSV-ish files, semi-structured config — anything that fits a single line and follows a shape regex can extract it.
Pattern: log entry
An apache-style log line: 192.168.1.1 - - [04/May/2026:14:32:11 +0000] "GET /api/data HTTP/1.1" 200 1234. Extracting fields:
(?P<ip>[\d.]+) - - \[(?P<date>[^\]]+)\] "(?P<method>\w+) (?P<path>[^ ]+) [^"]+" (?P<status>\d+) (?P<bytes>\d+)
Five named groups. The pattern reads almost like the spec.
Pattern: key=value pairs
Lines like name=Pippa age=3 mood=warm. Extract all pairs: (\w+)=(\S+) with findall returns [('name', 'Pippa'), ('age', '3'), ('mood', 'warm')].
If values can contain spaces (name="Pippa Choi"), upgrade: (\w+)=(?:"([^"]*)"|(\S+)) — captures either quoted or bare value.
The shape-first method
The pattern always starts with describing the SHAPE in English:
- Words, separator, words, separator, ...
- Identify each piece (digits? hex? quoted string? word?).
- Wrap each piece you'll use in a capturing group, named.
- Test against 3 inputs: known-good, known-bad, edge case.
This is how production extraction patterns get written.