0Pricing
Ruby Academy · Lesson

Named Groups and Anchors

Advanced regex.

Named Groups and Anchors is a free Ruby Academy lesson on CoddyKit — lesson 4 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.

Why Named Groups?

Numbered captures like m[1] become hard to read when a pattern grows. Named groups give each capture a meaningful label.

Syntax: (?<name>pattern).

m = /(?<year>\d{4})-(?<month>\d{2})/.match('2026-05')
puts(m[:year])
puts(m[:month])

Accessing Named Captures

Access named captures with a Symbol or String key: m[:name]. The method named_captures returns them all as a Hash.

m = /(?<user>\w+)@(?<host>\w+)/.match('alice@mail')
p m.named_captures

Named Groups with =~

When a regex literal with named groups appears on the left of =~, Ruby creates local variables for each name automatically.

if /(?<word>\w+)/ =~ 'hello'
  puts word
end

Anchors: Start and End

Anchors match positions rather than characters:

  • ^ start of a line
  • $ end of a line

They are crucial for validating that a whole input matches.

puts(/^abc/.match?('abcdef'))
puts(/def$/.match?('abcdef'))
puts(/^abc$/.match?('abcdef'))

String Anchors \A and \z

^ and $ match line boundaries, which matters for multiline strings. For the whole string, prefer \A (start) and \z (very end).

text = "first\nsecond"
puts(/^second/.match?(text))
puts(/\Asecond/.match?(text))

Validating Full Input

To check that an entire string matches a format, anchor both ends. This rejects strings with extra characters.

zip = /\A\d{5}\z/
puts(zip.match?('34000'))
puts(zip.match?('34000x'))

Word Boundaries

\b is a word boundary: the position between a word char and a non-word char. It matches whole words without consuming characters.

puts('cat catalog'.scan(/\bcat\b/).length)
puts('cat catalog'.scan(/cat/).length)

Lookahead

A positive lookahead (?=...) asserts what comes next without including it in the match. Negative lookahead is (?!...).

puts('100 dollars'.match(/\d+(?= dollars)/)[0])
puts('apple pie'.match?(/apple(?! juice)/))

Lookbehind

A lookbehind (?<=...) asserts what precedes the match. Negative lookbehind is (?<!...).

m = '$42'.match(/(?<=\$)\d+/)
puts(m[0])

Non-Capturing Groups

Sometimes you need grouping for alternation or quantifiers but do not want a capture. Use (?:...) for a non-capturing group.

m = /(?:Mr|Ms)\. (\w+)/.match('Ms. Lee')
p m.captures

Combining It All

A realistic validator uses anchors, named groups, and quantifiers together.

re = /\A(?<area>\d{3})-(?<num>\d{4})\z/
m = re.match('555-1234')
puts(m[:area])
puts(m[:num])

Quick Check

Which anchors should you use to ensure an entire string matches, ignoring line breaks?

Recap: Named Groups and Anchors

You mastered advanced regex tools:

  • Named groups (?<name>...) and named_captures
  • Anchors ^ $ \A \z \b
  • Lookahead (?=) / (?!) and lookbehind (?<=) / (?<!)
  • Non-capturing groups (?:...)
puts(/\A(?<id>\d+)\z/.match('42')[:id])

Frequently asked questions

Is the “Named Groups and Anchors” lesson free?

Yes — the full text of “Named Groups and Anchors” 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 “Named Groups and Anchors”?

Advanced regex. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Named Groups and Anchors” 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