0
0
Rubyprogramming~10 mins

Capture groups in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Capture groups
Start with regex pattern
Match string against pattern
Find capture groups in parentheses
Extract matched parts for each group
Use captured groups for output or processing
End
The regex engine matches the string, finds parts in parentheses as capture groups, and extracts them for use.
Execution Sample
Ruby
text = "Name: John, Age: 30"
pattern = /(Name: (\w+)), Age: (\d+)/
matches = text.match(pattern)
puts matches[1]
puts matches[2]
puts matches[3]
This code matches a string with a pattern having capture groups and prints each captured part.
Execution Table
StepActionEvaluationResult
1Define text stringtext = "Name: John, Age: 30"text holds the full string
2Define regex patternpattern = /(Name: (\w+)), Age: (\d+)/pattern has 3 capture groups
3Match text with patternmatches = text.match(pattern)matches is a MatchData object
4Access matches[1]matches[1]"Name: John" (whole first group)
5Access matches[2]matches[2]"John" (second group)
6Access matches[3]matches[3]"30" (third group)
7EndNo more stepsExecution complete
💡 All capture groups extracted and printed, match successful
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
text"Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30"
pattern/(Name: (\w+)), Age: (\d+)//(Name: (\w+)), Age: (\d+)//(Name: (\w+)), Age: (\d+)//(Name: (\w+)), Age: (\d+)//(Name: (\w+)), Age: (\d+)//(Name: (\w+)), Age: (\d+)/
matchesnilMatchData object with groups: ["Name: John", "John", "30"]SameSameSameSame
Key Moments - 3 Insights
Why does matches[1] return "Name: John" and not just "John"?
matches[1] returns the first capture group which is (Name: (\w+)), including "Name: John". The inner group (\w+) is matches[2], which returns just "John". See execution_table rows 4 and 5.
What happens if the pattern does not match the text?
If no match occurs, matches will be nil, so trying to access matches[1] would cause an error. The code assumes a successful match as shown in execution_table row 3.
Why do we use parentheses in the regex pattern?
Parentheses define capture groups that extract parts of the matched string separately. Without them, you get only the full match, not parts. This is shown in the pattern definition and extraction steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of matches[2] at step 5?
A"John"
B"Name: John"
C"30"
Dnil
💡 Hint
Check execution_table row 5 under Result column for matches[2]
At which step does the variable 'matches' become a MatchData object?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table row 3 Action and Result columns
If the text was "Name: Alice, Age: 25", what would matches[3] be?
A"John"
B"Alice"
C"25"
D"Name: Alice"
💡 Hint
matches[3] captures the age number as shown in execution_table row 6
Concept Snapshot
Capture groups in Ruby regex are parts of the pattern inside parentheses.
They extract specific parts of a matched string.
Use match method to get a MatchData object.
Access groups with matches[1], matches[2], etc.
matches[0] is the full match.
Groups are numbered by their opening parenthesis order.
Full Transcript
This example shows how Ruby uses capture groups in regular expressions. We start with a text string and a regex pattern with parentheses marking capture groups. When we match the pattern against the text, Ruby returns a MatchData object. This object holds the full match and each captured group separately. We access these groups by index: matches[1] for the first group, matches[2] for the second, and so on. The example extracts the name and age from the string and prints them. Key points include understanding that parentheses define groups, groups are numbered by their order, and the match must succeed to access groups safely.