Recall & Review
beginner
What is a capture group in Ruby regular expressions?
A capture group is a part of a regular expression enclosed in parentheses (). It saves the matched text so you can use it later.
Click to reveal answer
beginner
How do you access the text matched by the first capture group in Ruby?
You can access it using
$1 after the match or by using MatchData[1] if you use =~ or match methods.Click to reveal answer
beginner
What does this Ruby code output?<br>
text = 'Name: John' if text =~ /Name: (\w+)/ puts $1 end
It outputs
John because the capture group (\w+) matches the word after 'Name: ' and $1 holds that match.Click to reveal answer
intermediate
Can capture groups be nested in Ruby regular expressions?
Yes, capture groups can be nested. Each pair of parentheses creates a new group, and you can access them by their order of opening parentheses.
Click to reveal answer
intermediate
What is the difference between a capture group and a non-capturing group in Ruby regex?
A capture group saves the matched text for later use. A non-capturing group, written as
(?:...), groups parts of the pattern without saving the match.Click to reveal answer
In Ruby, what does the capture group
(\d+) match?✗ Incorrect
The pattern
\d+ matches one or more digits (0-9).How do you write a non-capturing group in Ruby regex?
✗ Incorrect
Non-capturing groups use
(?:...) to group without saving the match.After matching with
/Hello (\w+)/, how do you get the captured word in Ruby?✗ Incorrect
$1 holds the text matched by the first capture group.What will
/(a(b))/.match('ab')[2] return in Ruby?✗ Incorrect
The second capture group
(b) matches 'b', so match[2] returns 'b'.Which of these is true about capture groups in Ruby regex?
✗ Incorrect
Capture groups save the matched text so you can use it later in your code.
Explain what a capture group is and how you use it in Ruby regular expressions.
Think about how parentheses help you save parts of the matched text.
You got /3 concepts.
Describe the difference between a capturing group and a non-capturing group in Ruby regex.
One saves the match, the other just groups without saving.
You got /3 concepts.