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.
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"
.
Let's look at how we can implement the greedy quantifier in Ruby.
# Define our input string and regular expressionstring = "AAAAAB"regex = /A+/# Use greedy quantifier to match 'A'smatched = string.match(regex)# Output the matched stringputs "Matched string for greedy quantifier: #{matched}"
match
method to check if the string matches the regular expression.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.
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"
.
Let's look at how we can implement reluctant quantifiers in Ruby.
# Define our input string and regular expressionstring = "AAAAAB"regex = /A+?/# Use reluctant quantifier to match 'A'smatched = string.match(regex)# Output the matched stringputs "Matched string for reluctant quantifier: #{matched}"
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.
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.
Let's look at how we can implement possessive quantifiers in Ruby.
# Define our input string and non-possessive regular expressionstring = "AAA"non_possessive_regex = /A+./# Use the non-possessive expression to match 'A'snon_possessive_matched = string.match(non_possessive_regex)# Output the matched stringputs "Non-possessive matched string: #{non_possessive_matched}"# Define possessive regular expressionpossessive_regex = /A++./# Use the possessive expression to match 'A'spossessive_matched = string.match(possessive_regex)# Output the matched stringputs "Possessive matched string: #{possessive_matched}"
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.
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:
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. |
|
|
|
Free Resources