liveBook · Manning

Regular expression

As explained in Regular Expression Puzzles and AI Coding Assistants

A pattern notation and matching language for describing sets of strings and locating or transforming text that conforms to those patterns.

A regular expression is compact pattern notation for describing text to match, extract, validate, or transform. Its ideas are simple; the danger is that one changed character can produce a valid pattern with a different meaning. Treat a regex as a precise claim about every candidate string, not as punctuation that happened to work once. section 1.1 section 1.2

The greedy little trap

Suppose every target word starts with x and ends with y. x.*y takes as much text as possible before reaching a y; x.*?y takes as little as possible. Neither pattern, by itself, promises a whole-word match. Pause before choosing: what should happen between neighboring x…y words? section 2.1

pat1 = re.compile(r'x.*y')    #1
pat2 = re.compile(r'x.*?y')   #2

Compare the two nearly identical patterns character by character. The added ? changes how the wildcard chooses its span, not the surrounding requirement. section 2.1

Anchors and lookahead make local rules describe an entire string. For a domino-like encoding, each symbol can be checked against its permitted next symbol while consuming only one character; the final symbol needs its own end-of-string case. That avoids accidentally checking disjoint pairs instead of overlapping neighbors. section 3.8 section 3.11

Correctness has a performance clause. A mathematically correct validator with ambiguous repeated alternatives can backtrack catastrophically on an almost-valid message, taking roughly twice as long for each added character. Test failures as seriously as successes, especially long failures. section 3.1 section 3.0

A reliable working habit is:

  • State the language being accepted: allowed alphabet, boundaries, repetition, and adjacency rules. section 3.11
  • Try counterexamples that expose valid tails, missing terminators, overlapping relationships, and malformed near-matches. section 2.14 section 3.9
  • Inspect generated regexes rather than trusting plausible explanations or passing cherry-picked tests. section 3.12 section 4.24

Regex as a supporting tool

Regular expressions can reduce a structured representation before testing it. A poker full-house check strips suit symbols, then matches rank repetitions; surrounding Python handles sorting and a useful return value. Compactness is optional, not a moral duty. section 4.20 section 4.15

How Regular Expression Puzzles and AI Coding Assistants covers it

How deep it goes
Works through quantifiers, anchors, lookahead, backreferences, edge cases, and catastrophic backtracking rather than offering an exhaustive syntax reference.
Examples
Uses x…y words, telomere sequences, message protocols, domino plays, signal art, poker hands, and substring counting.
What it assumes
Assumes some programming familiarity while offering a refresher for users not yet comfortable with regex intricacies.
What you can do after
Evaluate/understand regex solutions by testing boundaries, overlap, malformed inputs, and performance rather than accepting plausible output.
How it connects
Connects regular expressions with quantifiers, anchors, lookahead, backreferences, Python re, parser grammars, and AI coding assistants.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Regular Expression Puzzles and AI Coding Assistants. The section links point to the parts of the book it draws on.