Bird
Raised Fist0
Rubyprogramming~20 mins

Capture groups in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Capture Groups Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of capture groups in Ruby regex
What is the output of this Ruby code snippet?
Ruby
text = "Name: John, Age: 30"
match = /Name: (\w+), Age: (\d+)/.match(text)
puts match[1]
puts match[2]
AJohn\n30
BName\nAge
CJohn, Age: 30
Dnil\nnil
Attempts:
2 left
💡 Hint
Remember that capture groups are accessed by their index starting at 1.
❓ Predict Output
intermediate
2:00remaining
Using named capture groups in Ruby
What will this Ruby code print?
Ruby
text = "Product: Book, Price: 15"
regex = /Product: (?<product>\w+), Price: (?<price>\d+)/
match = regex.match(text)
puts match[:product]
puts match[:price]
Aproduct\nprice
BBook\n15
CBook, Price: 15
Dnil\nnil
Attempts:
2 left
💡 Hint
Named capture groups can be accessed by symbol keys.
❓ Predict Output
advanced
2:00remaining
Accessing a capture group index beyond available groups
What does this Ruby code print?
Ruby
text = "Date: 2024-06-15"
match = /Date: (\d{4})-(\d{2})-(\d{2})/.match(text)
p match[4]
AIndexError
BTypeError
Cnil
DNoMethodError
Attempts:
2 left
💡 Hint
Check how many capture groups are in the regex and the index used.
🧠 Conceptual
advanced
2:00remaining
Effect of non-capturing groups on match data
Given the regex /a(?:b)c(d)/ applied to the string "abcd", how many capture groups does the match have?
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Non-capturing groups do not count as capture groups.
❓ Predict Output
expert
3:00remaining
Output of nested capture groups in Ruby
What is the output of this Ruby code?
Ruby
text = "abc123xyz"
regex = /(a(b(c)))(\d+)(x(y)z)/
match = regex.match(text)
puts match[0]
puts match[1]
puts match[2]
puts match[3]
puts match[4]
puts match[5]
puts match[6]
Aabc123xyz\nabc\nbc\nc\n123\nxyz\ny
Babc123xyz\nabc\nbc\nc\n123\nxyz\nnil
Cabc123xyz\nabc\nbc\nnil\n123\nxyz\ny
Dabc123xyz\nabc\nnil\nc\n123\nxyz\ny
Attempts:
2 left
💡 Hint
Nested capture groups are numbered from left to right by their opening parenthesis.

Practice

(1/5)
1. What does a capture group do in a Ruby regular expression like /(\d{3})-(\d{2})/?
easy
A. It repeats the pattern multiple times.
B. It saves parts of the matched text for later use.
C. It matches only digits without saving.
D. It ignores the matched text.

Solution

  1. Step 1: Understand capture groups in regex

    Capture groups are parts of a pattern enclosed in parentheses that save matched text.
  2. Step 2: Analyze the example pattern

    The pattern /(\d{3})-(\d{2})/ has two capture groups for three digits and two digits separated by a dash.
  3. Final Answer:

    It saves parts of the matched text for later use. -> Option B
  4. Quick Check:

    Capture groups = save matched parts [OK]
Hint: Parentheses in regex save matched text as groups [OK]
Common Mistakes:
  • Thinking capture groups repeat patterns
  • Confusing capture groups with non-capturing groups
  • Believing capture groups ignore matched text
2. Which of the following is the correct syntax to create a capture group in a Ruby regex?
easy
A. /(abc)/
B. /[abc]/
C. /{abc}/
D. /abc/

Solution

  1. Step 1: Identify capture group syntax

    Capture groups in Ruby regex are created by enclosing the pattern in parentheses ().
  2. Step 2: Compare options

    /(abc)/ uses parentheses correctly. /[abc]/ uses square brackets which match any one character. /{abc}/ uses curly braces which are quantifiers. /abc/ has no capture group.
  3. Final Answer:

    /(abc)/ -> Option A
  4. Quick Check:

    Parentheses = capture group [OK]
Hint: Use parentheses () for capture groups in regex [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Using curly braces {} incorrectly
  • Forgetting parentheses for capture groups
3. What will be the output of this Ruby code?
text = "Order 123-45"
match = text.match(/(\d{3})-(\d{2})/)
[match[0], match[1], match[2]]
medium
A. ["123-45", "45", "123"]
B. ["12345", "123", "45"]
C. ["Order 123-45", "123", "45"]
D. ["123-45", "123", "45"]

Solution

  1. Step 1: Understand match and capture groups

    The match method returns a MatchData object. match[0] is the full matched string, match[1] and match[2] are the first and second capture groups.
  2. Step 2: Apply to given string and pattern

    The pattern matches "123-45" in the text. The first group captures "123", the second "45".
  3. Final Answer:

    ["123-45", "123", "45"] -> Option D
  4. Quick Check:

    Full match + groups = ["123-45", "123", "45"] [OK]
Hint: match[0] full, match[1..] capture groups [OK]
Common Mistakes:
  • Confusing full match with capture groups
  • Swapping capture group order
  • Ignoring the dash in the match
4. What is wrong with this Ruby code snippet?
text = "abc123"
match = text.match(/(\d{3})/)
puts match[1]
medium
A. It prints "123" correctly.
B. It raises an error because there is no match.
C. It prints nil without error.
D. It raises a syntax error.

Solution

  1. Step 1: Check the regex and string

    The regex /(\d{3})/ looks for exactly three digits. The string "abc123" contains "123" which matches.
  2. Step 2: Understand match and output

    The match object captures "123" in group 1. puts match[1] prints "123".
  3. Final Answer:

    It prints "123" correctly. -> Option A
  4. Quick Check:

    Digits found and printed = "123" [OK]
Hint: Check if regex matches before accessing groups [OK]
Common Mistakes:
  • Assuming no match causes error
  • Expecting nil instead of matched string
  • Confusing syntax error with runtime error
5. Given a string of dates like "2023-04-01, 2024-12-31", which Ruby code correctly extracts all year parts using capture groups?
hard
A. dates.scan(/\d{4}-\d{2}-\d{2}/)
B. dates.match(/(\d{4})-\d{2}-\d{2}/)
C. dates.scan(/(\d{4})-\d{2}-\d{2}/).flatten
D. dates.match(/\d{4}/)

Solution

  1. Step 1: Understand the goal and methods

    We want all years from multiple dates. scan finds all matches, match finds only first.
  2. Step 2: Analyze each option

    dates.scan(/(\d{4})-\d{2}-\d{2}/).flatten uses scan with a capture group for year, returning all years as array. dates.match(/(\d{4})-\d{2}-\d{2}/) uses match which returns only first match. dates.scan(/\d{4}-\d{2}-\d{2}/) uses scan but no capture group, returns full dates. dates.match(/\d{4}/) uses match without capture group, only first year.
  3. Final Answer:

    dates.scan(/(\d{4})-\d{2}-\d{2}/).flatten -> Option C
  4. Quick Check:

    scan + capture group = all years [OK]
Hint: Use scan with capture groups to get all matches [OK]
Common Mistakes:
  • Using match instead of scan for multiple matches
  • Not using capture groups to extract parts
  • Forgetting to flatten nested arrays from scan
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard