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
