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


























