Bird
0
0

Fix the error in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Regular Expressions
Fix the error in this Ruby code snippet:
data = 'abc123'
match = data.match(/(abc)(\d+)/)
puts match[0]
The goal is to print only the digits.
AChange regex to /(abc)(\d+)/i
BChange puts match[0] to puts match[2]
CUse scan instead of match
DAdd another capture group for digits
Step-by-Step Solution
Solution:
  1. Step 1: Understand match indices

    match[0] is full match 'abc123', match[2] is digits captured by second group.
  2. Step 2: Correct code to print digits

    Change puts match[0] to puts match[2] to print only digits.
  3. Final Answer:

    Change puts match[0] to puts match[2] -> Option B
  4. Quick Check:

    Digits captured in group 2 accessed by match[2] [OK]
Quick Trick: Full match is [0], capture groups start at [1] [OK]
Common Mistakes:
  • Printing full match instead of capture group
  • Adding unnecessary capture groups
  • Changing regex flags without need

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes