Bird
0
0

Why does scan return only the last match when using a regex with capturing groups?

hard📝 Conceptual Q10 of 15
Ruby - Regular Expressions
Why does scan return only the last match when using a regex with capturing groups?
ABecause scan only works without groups
BBecause scan ignores all but the last match
CBecause scan returns a string, not an array
DBecause scan returns an array of capture groups, each as an array
Step-by-Step Solution
Solution:
  1. Step 1: Understand scan with capturing groups

    When regex has groups, scan returns an array of arrays, each inner array has group matches.
  2. Step 2: Clarify why last match confusion occurs

    If you access only one element, it may seem like last match only, but scan returns all matches grouped.
  3. Final Answer:

    Because scan returns an array of capture groups, each as an array -> Option D
  4. Quick Check:

    scan with groups returns array of arrays = D [OK]
Quick Trick: scan with groups returns array of arrays, not single string [OK]
Common Mistakes:
  • Thinking scan returns only last match
  • Confusing return type as string
  • Believing scan ignores groups

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes