liveBook · Manning

Non-greedy quantifier

A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match.

How 2 Manning books explain it

Secrets of the JavaScript Ninja, Third Edition

A non-greedy operator is a regular-expression quantifier that initially matches as little text as possible while allowing the pattern to match.

How it is used
Selects the first suitable stopping point when matching repeated characters in delimited text.
How deep it goes
Defines the ? suffix and demonstrates its effect, then contrasts greedy backtracking without tracing non-greedy execution in detail.
Examples
Contrasts /<.*>/ with /<.*?>/ on two angle-bracketed fragments and compares greedy and non-greedy letter matching.
What you can do after
Apply it via JavaScript regular expressions by appending ? to a repetition operator when the earliest suitable match is wanted.

Read how Secrets of the JavaScript Ninja, Third Edition explains non-greedy quantifier →

The Well-Grounded Rubyist, Fourth Edition

A non-greedy quantifier matches the fewest repetitions needed for the remainder of the regular expression to match.

How it is used
Limits how far a quantified pattern extends while allowing the remainder of the expression to match.
How deep it goes
Defines `*?` and `+?`, contrasts greedy matching, and explains backtracking with a digit example.
Examples
Demonstrates stopping at the first exclamation point, matching digits, and capturing parenthesized text across newlines.
What you can do after
Apply `*?` or `+?` in Ruby regular expressions to limit a match while satisfying the remaining pattern.

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

Also covered in

What to know first

Related concepts

Questions about non-greedy quantifier

What is non-greedy quantifier?

A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match.

Which Manning books explain non-greedy quantifier?

Secrets of the JavaScript Ninja, Third Edition and The Well-Grounded Rubyist, Fourth Edition.

What should I know before learning non-greedy quantifier?

Manning books that teach non-greedy quantifier usually introduce regular expression, regex grouping and lookbehind assertion first.

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

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

What is non-greedy quantifier a kind of?

Non-greedy quantifier 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.