What are the three types of quantifiers for RegEx in Ruby?

In regex, a quantifier is a meta-character that tells the regex engine how many times to match the character, token, or sub-expression directly before that quantifier.

Ruby supports three distinct types of quantifiers: greedy, reluctant, and possessive. Let’s see how we can use these regex quantifiers to match patterns in Ruby.

Greedy

A greedy quantifier tries to match as many elements as possible. Hence, it is known as greedy because it captures the longest possible string. An example of this greedy behavior can be observed in the + quantifier, which tries to match one or more instances of the expression directly before the +. The regular expression A+ (exhibiting greedy behavior) would then match "AAA" from the string "AAA".

Implementation

Let’s look at how we can implement the greedy quantifier in Ruby.

Read more here: Source link