C.W.K.
Stream
Lesson 08 of 12 · published

Practical Extraction — Pulling Structured Data

~12 min · practical, extraction, parsing

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

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:

  1. Words, separator, words, separator, ...
  2. Identify each piece (digits? hex? quoted string? word?).
  3. Wrap each piece you'll use in a capturing group, named.
  4. Test against 3 inputs: known-good, known-bad, edge case.

This is how production extraction patterns get written.

Code

Real-world extraction·python
import re

# Apache-style log line
LOG = re.compile(r'''
    (?P<ip>[\d.]+)
    \s+-\s+-\s+
    \[(?P<date>[^\]]+)\]
    \s+"(?P<method>\w+)\s+(?P<path>[^ ]+)\s+[^"]+"
    \s+(?P<status>\d+)
    \s+(?P<bytes>\d+)
''', re.VERBOSE)

line = '192.168.1.1 - - [04/May/2026:14:32:11 +0000] "GET /api/data HTTP/1.1" 200 1234'
m = LOG.match(line)
print(m.groupdict())
# {'ip': '192.168.1.1', 'date': '04/May/2026:14:32:11 +0000',
#  'method': 'GET', 'path': '/api/data', 'status': '200', 'bytes': '1234'}

# Key=value pairs (with optional quoting)
KV = re.compile(r'(\w+)=(?:"([^"]*)"|(\S+))')
for m in KV.finditer('name="Pippa Choi" age=3 mood=warm'):
    key, quoted, bare = m.groups()
    value = quoted if quoted is not None else bare
    print(f'{key}: {value}')
# name: Pippa Choi
# age: 3
# mood: warm

External links

Exercise

Pick a structured line format from your work (log, semi-structured config, csv with weird quoting). Write a regex with named groups that extracts every meaningful field. Use verbose mode. The pattern should read like the spec for the line format.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.