C.W.K.
Stream
Lesson 01 of 10 · published

The Problem: Finding Patterns in Text

~10 min · intro, motivation, use-cases

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

Why a tiny language for text shapes exists

Imagine you have a 4 GB log file and you need every line that mentions an IP address followed by a 5xx HTTP status. You can write a Python script that splits each line, parses the third column, checks if it's an IP, then checks if the status starts with 5. Twenty lines of code, and you'll spend half an hour debugging the column index.

Or you write \b\d+\.\d+\.\d+\.\d+\b.*\b5\d{2}\b and pipe the file through ripgrep. One line. Half a second.

Regular expressions exist for that gap — when the question is "which strings have this shape?", hand-rolled parsing is overkill. Regex is a pattern grammar: a tiny language whose only job is to describe shapes of text.

Three things regex is genuinely good at

Search. "Find every string in this haystack that looks like X." Editors, IDEs, log analyzers, security scanners.

Validation. "Does this whole string look like a valid X?" Form fields, config parsers, route matchers.

Extraction + replacement. "Pull the X out of this string" or "replace every X with Y." Data cleaning, code mods, build scripts.

Notice what's not on that list: parsing nested structures, tracking semantics, anything HTML or JSON. We'll come back to that hard limit at the end of this track.

Code

The script you'd write without regex·python
# Find lines with an IP address followed somewhere by a 5xx status
import re_disabled  # pretend regex doesn't exist

with open('access.log') as f:
    for line in f:
        parts = line.split()
        if len(parts) < 9:
            continue
        ip = parts[0]
        status = parts[8]
        # Now you write your own IP validator and status check...
        # Twenty more lines, three off-by-one bugs.
The same thing with regex·bash
# One line. Streams a 4 GB file in seconds.
rg '\b\d+\.\d+\.\d+\.\d+\b.*\b5\d{2}\b' access.log

External links

Exercise

Open any plain-text file you have lying around (a log, a CSV export, a chat transcript). Pick three different *shapes* of text in it that you'd want to find — phone numbers, dates, URLs, names with capital letters. For each, write one English sentence describing the shape *exactly*. You'll spend the rest of the quest learning to translate those sentences into patterns.

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.