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.

# Define our input string and regular expression
string = "AAAAAB"
regex = /A+/
# Use greedy quantifier to match 'A's
matched = string.match(regex)
# Output the matched string
puts "Matched string for greedy quantifier: #{matched}"

Explanation

  • Line 2: We create a variable to store our input string.
  • Line 3: We create a variable to store our regular expression, which adopts a greedy approach.
  • Line 5: We use the match method to check if the string matches the regular expression.
  • Line 9: We use puts to print the matched string for the greedy approach (if any).

Feel free to modify the string and regex variables to learn more about greedy quantifiers.

Reluctant

A reluctant quantifier tries to match as few elements as possible. Hence, it is known as reluctant because it tries to capture the shortest possible string. Generally, a regular expression can be made reluctant by appending the ? quantifier to it. Thus the regular expression A+? (exhibiting reluctant behavior) would then match "A" from the string "AAA".

Implementation

Let's look at how we can implement reluctant quantifiers in Ruby.

# Define our input string and regular expression
string = "AAAAAB"
regex = /A+?/
# Use reluctant quantifier to match 'A's
matched = string.match(regex)
# Output the matched string
puts "Matched string for reluctant quantifier: #{matched}"

Explanation

  • Line 3: We create a variable to store our regular expression, which adopts a reluctant approach.
  • Line 9: We use puts to print the matched string for the reluctant approach (if any).

Feel free to modify the string and regex variables to learn more about reluctant quantifiers.

Possessive

A possessive quantifier tries to uncompromisingly match as many elements as possible. Hence, it is known as possessive because once it captures a portion of the string, it will not backtrack or relinquish its hold, even if doing so would allow the overall regular expression to succeed. This behavior is helpful to prevent the regex engine from backtracking to optimize performance.

Generally, a quantifier becomes possessive when an + is appended. To illustrate this possessive behavior, consider the input string "AAA" and the regular expression A+.. The . matches any character except for line breaks. The + quantifier would greedily match the entire string but then relinquish the final “A” to allow the . to match, ensuring the regular expression’s success. In contrast, the possessive expression A++. would not find a match in the input string because the + quantifier now refuses to release the final "A" for the . to match.

Implementation

Let's look at how we can implement possessive quantifiers in Ruby.

# Define our input string and non-possessive regular expression
string = "AAA"
non_possessive_regex = /A+./
# Use the non-possessive expression to match 'A's
non_possessive_matched = string.match(non_possessive_regex)
# Output the matched string
puts "Non-possessive matched string: #{non_possessive_matched}"
# Define possessive regular expression
possessive_regex = /A++./
# Use the possessive expression to match 'A's
possessive_matched = string.match(possessive_regex)
# Output the matched string
puts "Possessive matched string: #{possessive_matched}"

Explanation

  • Line 3: We create a variable to store our regular expression, which adopts a non-possessive approach.

  • Line 6: We use the match method to check if the string matches the regular expression for the non-possessive approach.

  • Line 9: We use puts to print the matched string. In this case, we match we the entire string.

  • Line 13: We create a variable to store our regular expression, which adopts a possessive approach.

  • Line 16: We use the match method to check if the string matches the regular expression for the possessive approach.

  • Line 19: We use puts to print the matched string. As our regular expression fails due to it's possessive approach, our output is an empty string.

Feel free to modify the string, possessive_regex, and non_possessive_regex variables and to learn more about possessive quantifiers.

Summary

Suppose we have an input "AAA" string. The differences between how greedy, reluctant, and possessive quantifiers would match this input are summarized in the table below:

Differences Between Quantifiers

Greedy

Reluctant

Possessive

This quantifier matches as many elements as possible.

This quantifier matches as few elements as possible.

This quantifier matches as many elements as possible but will not release them for subsequent tokens.

A+ matches the "AAA" string.

A*? matches an empty string.

A++ matches "AAA" and A++. Matches an empty string.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved