0Pricing
Ruby Academy · Lesson

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.captures

Destructuring 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 host

Pre and Post Match

MatchData has helpers for the text around the match:

  • pre_match the part before
  • post_match the 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 nums

scan 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 pairs

Safely 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'
end

Special 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
end

Extracting 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:

  • match returns MatchData or nil
  • Index 0 is the whole match, 1+ are groups
  • captures returns all groups as an array
  • scan finds 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

  1. Regex Literals
  2. Matching and Capturing
  3. Substitution
  4. Named Groups and Anchors
← Back to Ruby Academy