liveBook · Manning

Greedy matching

Greedy matching is regular-expression behavior in which a quantifier matches as much text as possible while still allowing the overall pattern to match.

How 4 Manning books explain it

Secrets of the JavaScript Ninja, Third Edition

Greedy repetition consumes as many consecutive characters satisfying its term as possible while still allowing the overall match.

How it is used
Uses greedy repetition to take the longest tentative match while leaving later regex terms a chance to match.
How deep it goes
Explains consumption and character-by-character backtracking, then illustrates the performance risk with a timed pattern.
Examples
Compares bracketed-string matches, traces the match of `27 dresses`, and times rejection by a repeated-term regex.
What you can do after
Diagnose/debug with it when a regex consumes more text than intended or takes unexpectedly long to reject input.

Read how Secrets of the JavaScript Ninja, Third Edition explains greedy matching →

LLM Evaluation and Alignment, The Foundational Ideas

Greedy matching is a matching strategy that selects the best available correspondence for each item without globally optimizing all correspondences jointly.

How it is used
Evaluates semantic similarity between candidate and reference tokens within BERTScore.
How deep it goes
Defines embeddings, pairwise cosine similarities, recall, precision, and IDF weighting; it stops short of judging factual correctness.
Examples
Uses “The weather is cold today” versus “It is freezing today,” including a 0.759 unweighted recall example.
What you can do after
Apply it via BERTScore to compare paraphrased or reordered candidate text against a reference.

Read how LLM Evaluation and Alignment, The Foundational Ideas explains greedy matching →

The Well-Grounded Rubyist, Fourth Edition

A greedy quantifier matches as many repetitions as possible while still allowing the remainder of the regular expression to match.

How it is used
Uses greedy and non-greedy quantifiers to control how regular expressions consume repeated characters.
How deep it goes
Explains default greediness, lazy modifiers, captures, and backtracking, stopping short of broader regexp-engine implementation.
Examples
Demonstrates digit runs, exclamation-delimited strings, and MatchData captures showing backtracking.
What you can do after
Diagnose regexp matches by predicting greedy consumption, applying lazy modifiers, and inspecting captures after backtracking.

Read how The Well-Grounded Rubyist, Fourth Edition explains greedy matching →

Tiny Python Projects: Learn coding and testing with puzzles and games

A regular-expression matching behavior in which a quantifier selects the longest possible substring that still permits the pattern to match.

How it is used
Uses greedy matching while extracting angle-bracket placeholders from Mad Libs input.
How deep it goes
Explains greedy, non-greedy, and delimiter-excluding patterns with interactive examples, but does not cover regex-engine internals.
Examples
Demonstrates `<.+>`, `<.+?>`, and `<[^<>]+>` against placeholder text and unbalanced brackets.
What you can do after
Diagnose/debug with Python regular expressions by identifying overmatching and choosing non-greedy or delimiter-excluding patterns.

Read how Tiny Python Projects: Learn coding and testing with puzzles and games explains greedy matching →

Also covered in

What to know first

Related concepts

Questions about greedy matching

What is greedy matching?

Greedy matching is regular-expression behavior in which a quantifier matches as much text as possible while still allowing the overall pattern to match.

Which Manning books explain greedy matching?

Secrets of the JavaScript Ninja, Third Edition, LLM Evaluation and Alignment, The Foundational Ideas, The Well-Grounded Rubyist, Fourth Edition and Tiny Python Projects: Learn coding and testing with puzzles and games.

What should I know before learning greedy matching?

Manning books that teach greedy matching usually introduce regex grouping, Python and regular expression first.

What is the difference between greedy matching and non-greedy quantifier?

Greedy matching is regular-expression behavior in which a quantifier matches as much text as possible while still allowing the overall pattern to match. A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match.

What is greedy matching a kind of?

Greedy matching is a kind of quantifier. A regular-expression quantifier is syntax that specifies how many times a preceding pattern element may or must match.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.