Matching and Capturing
match and captures.
Matching and Capturing is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
MatchData Objects
When you call regex.match(string) Ruby returns a MatchData object on success, or nil on failure.
MatchData holds the matched text and any captured groups.
m = /world/.match('hello world')
puts m.class
puts(m[0])The Whole Match: index 0
Index 0 of a MatchData is always the entire matched substring, regardless of any groups.
m = /\d+/.match('order 4521 shipped')
puts(m[0])
puts(m.to_s)Capturing Groups
Parentheses ( ) create capturing groups. Each group is accessible by number starting from 1.
m = /(\d{4})-(\d{2})/.match('2026-05')
puts(m[1])
puts(m[2])The captures Method
captures returns an array of all captured groups (excluding the whole match at index 0).
m = /(\w+)@(\w+)/.match('alice@example')
p m.capturesDestructuring Captures
Because captures returns an array, you can destructure the result directly into variables.
user, host = /(\w+)@(\w+)/.match('bob@server').captures
puts user
puts hostPre and Post Match
MatchData has helpers for the text around the match:
pre_matchthe part beforepost_matchthe part after
m = /\d+/.match('abc123xyz')
puts(m.pre_match)
puts(m.post_match)scan for All Matches
String#scan returns an array of every match in the string, not just the first one.
nums = '12 cats, 8 dogs, 3 birds'.scan(/\d+/)
p numsscan with Groups
If your pattern has groups, scan returns an array of arrays, one per match, each holding that match's captures.
pairs = 'a=1 b=2 c=3'.scan(/(\w)=(\d)/)
p pairsSafely Handling nil
Always remember match returns nil when nothing matches. Guard against it before using the result.
m = /(\d+)/.match('no digits here')
if m
puts m[1]
else
puts 'no match found'
endSpecial Variables
After a successful match, Ruby sets global variables: $~ is the MatchData, and $1, $2 are the numbered captures.
These are handy in quick scripts but explicit MatchData is clearer in real code.
if 'name: Alice' =~ /name: (\w+)/
puts $1
endExtracting a Date
Combine groups and captures to pull structured data from a string in one step.
year, month, day = /(\d{4})-(\d{2})-(\d{2})/.match('2026-05-29').captures
puts "Year #{year}, Month #{month}, Day #{day}"Quick Check
Given 'a=1 b=2'.scan(/(\w)=(\d)/), what is the shape of the result?
Recap: Matching and Capturing
You can now extract data from matches:
matchreturns MatchData or nil- Index 0 is the whole match, 1+ are groups
capturesreturns all groups as an arrayscanfinds every match in the string- Always guard against
nil
p 'x1 y2 z3'.scan(/([a-z])(\d)/)Frequently asked questions
Is the “Matching and Capturing” lesson free?
Yes — the full text of “Matching and Capturing” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Matching and Capturing”?
match and captures. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Matching and Capturing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex Literals
- Matching and Capturing
- Substitution
- Named Groups and Anchors