Named Groups and Anchors
Advanced regex.
Named Groups and Anchors is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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_capturesNamed 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
endAnchors: 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.capturesCombining 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>...)andnamed_captures - Anchors
^ $ \A \z \b - Lookahead
(?=)/(?!)and lookbehind(?<=)/(?<!) - Non-capturing groups
(?:...)
puts(/\A(?<id>\d+)\z/.match('42')[:id])Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Named Groups and Anchors” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Named Groups and Anchors”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- Regex Literals
- Matching and Capturing
- Substitution
- Named Groups and Anchors