liveBook · Manning

Regex engine

As explained in The Well-Grounded Rubyist, Fourth Edition

A regular-expression engine is software that interprets regular-expression patterns and searches input for matches.

A regex engine interprets a pattern and searches input for a match. Its behavior becomes visible when quantifiers compete with the rest of a pattern: a greedy quantifier takes as much as possible, then backtracks when that choice prevents a successful overall match. section 11.5.2

Ruby regexp quantifiers support several repetition strategies:

  • Greedy .+! matches through the last possible exclamation point; non-greedy .+?! stops at the first. section 11.5.2
  • \d+? can stop after the first digit, while \d+5 backtracks so \d+ matches 234 and 5 matches separately. section 11.5.2
  • Curly braces specify exact or bounded repetition, such as \d{3}-\d{4}, \d{1,10}, and \d{3,}. section 11.5.2

Captures reveal the engine's choice

Matching /(\d+)(5)/ against "Digits-R-Us 2345" produces "2345", with captures "234" and "5". Grouping the repetition as /([A-Z]{5})/ captures "BLACK" rather than only the final repeated character. section 11.5.2

>> string = "abc!def!ghi!"
=> "abc!def!ghi!"
>> /.+!/.match(string)[0]
=> "abc!def!ghi!"

Compare the greedy listing with the non-greedy variant to observe how the selected stopping point changes. section 11.5.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Descends from quantifier syntax into backtracking and captures, including exact counts and ranges.
Examples
Uses Ruby matches for exclamation-delimited strings, digit sequences, phone-like numbers, and uppercase words.
What you can do after
Apply it via Ruby regexp syntax to control repetition, inspect captures, and understand backtracking results.
How it connects
Connects greedy quantifiers with non-greedy quantifiers, backtracking, captures, and MatchData.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.