Bird
0
0

Why does this Ruby code raise an error?

medium📝 Debug Q7 of 15
Ruby - Regular Expressions
Why does this Ruby code raise an error?
regex = /\w+{2,4}/
"test" =~ regex
AQuantifier {2,4} is incorrectly applied to \w+
BMissing escape for +
CRegex delimiters are wrong
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Analyze quantifier usage in regex

    \w+ means one or more word characters. Applying {2,4} directly after + is invalid because + is already a quantifier.
  2. Step 2: Understand correct quantifier placement

    Quantifiers cannot be stacked like \w+{2,4}. Instead, use \w{2,4} to match 2 to 4 word characters.
  3. Final Answer:

    Quantifier {2,4} incorrectly applied after + quantifier -> Option A
  4. Quick Check:

    Quantifiers cannot stack like +{2,4} [OK]
Quick Trick: Don't stack quantifiers like +{2,4} in regex [OK]
Common Mistakes:
  • Stacking quantifiers incorrectly
  • Misunderstanding quantifier precedence
  • Assuming + can be escaped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes