0Pricing
DevOps Bootcamp · Lesson

Capture Groups, Backreferences & Substitution

Use parentheses to capture parts of a match and reuse them with backreferences for powerful matching and text substitution.

Capture Groups, Backreferences & Substitution is a free DevOps Bootcamp 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 DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Capture Groups?

Parentheses ( ) create a capture group that remembers the text it matched so you can reference it later.

echo "2026-05-29" | grep -E "([0-9]{4})-([0-9]{2})-([0-9]{2})"

Numbering of Groups

Groups are numbered left to right by their opening parenthesis. Group 1 is the first (, group 2 the second, and so on.

# (year)(month)(day)
# group 1 = year, 2 = month, 3 = day

Backreferences in Matching

A backreference like \1 matches the SAME text a previous group captured. Great for finding duplicates.

echo "hello world world" | grep -E "(\w+) \1"

Detecting Doubled Words

The pattern (\w+) \1 finds a word followed by itself, a common typo.

echo "the the cat sat" | grep -oE "(\w+) \1"

Capture Groups with sed

In sed substitutions, captured groups are reused in the replacement as \1, \2. Use -E for extended regex.

echo "2026-05-29" | sed -E "s|([0-9]{4})-([0-9]{2})-([0-9]{2})|\3/\2/\1|"

Swapping Fields

Capture two fields and reorder them in the replacement to transform data formats.

echo "Doe, John" | sed -E "s/(\w+), (\w+)/\2 \1/"

Non-Capturing Concept

Sometimes you group only to apply a quantifier, not to capture. In PCRE you can write (?:...) to avoid numbering.

# PCRE only (grep -P):
echo "abcabc" | grep -P "(?:abc){2}"

Alternation Inside Groups

Combine groups with alternation | to match one of several options while still capturing the result.

echo "cat dog fish" | grep -oE "(cat|dog)"

Quantifying a Group

Apply quantifiers to a whole group to repeat a pattern. (ab)+ matches one or more ab sequences.

echo "ababab" | grep -oE "(ab)+"

Capturing in awk

awk uses match() with RSTART and RLENGTH to extract the matched portion.

echo "id=42" | awk "{ if (match(\$0, /id=[0-9]+/)) print substr(\$0, RSTART, RLENGTH) }"

Practical Reformatting

Capture groups shine when reformatting logs or CSV columns into a new layout in a single pass.

echo "John,30,NYC" | sed -E "s/([^,]+),([^,]+),([^,]+)/Name: \1 Age: \2 City: \3/"

Quick Check

Test your capture group knowledge.

Recap

You mastered capture groups:

  • Parentheses capture and number matched text
  • Backreferences \1 match the same text again
  • In sed, reuse groups in replacements to reorder fields
  • Quantify and alternate inside groups for flexible patterns

Frequently asked questions

Is the “Capture Groups, Backreferences & Substitution” lesson free?

Yes — the full text of “Capture Groups, Backreferences & Substitution” is free to read here on the web, and the DevOps Bootcamp 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 DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Capture Groups, Backreferences & Substitution”?

Use parentheses to capture parts of a match and reuse them with backreferences for powerful matching and text substitution. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp 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 “Capture Groups, Backreferences & Substitution” 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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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. Introduction to Regular Expressions (Regex)
  2. Advanced Grep with Regex
  3. Combining Regex with sed/awk
  4. Capture Groups, Backreferences & Substitution
← Back to DevOps Bootcamp