The solution
Parsing real-world text often needs one regex to recognize a structured item — codes like H1 or (AH2b+EUH3) — and capture the message that follows. Written as one line, that pattern turned into a dense wall of punctuation: nested groups, lookaheads and quantifiers with no room for comments, and every change meant untangling the whole string. The obvious fixes each carried a cost: keep the one-liner and every edit stays risky; hand-write verbose mode and the same subpattern is duplicated wherever it appears; add a regex-building library and you learn new syntax plus a dependency.
The move was to treat the pattern as code. The author defined each logical unit once — a code fragment like [A-Z]*H, the multicode that wraps codes separated by plus signs, then the full matcher — each as a raw verbose string with comments on every line, composing larger patterns from smaller ones via {code} placeholders. Because re.VERBOSE ignores whitespace and hash comments, each fragment reads top-down: parentheses and alternations become visible structure instead of glyph soup. Because f-strings resolve at build time, what reaches the re module is still one plain pattern string.
The payoff is exactly where one-line regexes hurt. Each fragment is defined once, so a grammar change updates every pattern that embeds it; fragments can be tested alone with re.findall before they are joined; and nothing new entered the stack — the author used two long-coexisting standard-library features and admits it took him three years to see the combination. The post documents the traps: hashes and spaces need escaping or character classes, brace quantifiers must be doubled inside f-strings, and inline (?x) flags keep verbose mode working when the caller controls the flags.
What it achieved
Structure the regex like code: define each fragment once as a commented verbose string and compose the final pattern with f-strings
Why it worked
re.VERBOSE already ignores whitespace and comments, so each fragment reads like documented code using only the standard library.
F-string interpolation splices fragments at build time, leaving a plain pattern string any re call accepts unchanged.
Each fragment is defined once, so a grammar change updates every composed pattern instead of repeated inline copies.
Fragments are testable in isolation before composition, so failures localize to the join points between them.
Zero dependencies and no new DSL mean the approach transfers to any Python 3.6+ codebase without setup.
What can be applied
When one pattern resists maintenance, don't add a library: combine features you already have — verbose comments for readability, interpolation for reuse — with zero dependencies.
Aftermath
Published in May 2022, it drew 304 points and 90 comments on Hacker News; reactions split between treating the post as a revelation and noting that splitting regexes into reusable subpatterns was already common practice. Commenters pointed to stronger mechanisms — PCRE's and Perl's DEFINE subroutine blocks, and the third-party regex module — for reuse without interpolation, and the author appended a bonus section showing the same parser written with DEFINE. The post remains live with those caveats documented and its core claim intact: composable, commented patterns with zero dependencies.
FOLLOW THE EVIDENCE
The sources
- The unreasonable effectiveness of f-strings and re.VERBOSE death.andgravity.com
- The unreasonable effectiveness of f-strings and re.verbose — Hacker News discussion news.ycombinator.com