100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Backreferences and Groups

Master capturing parts of a match with groups and reusing them via backreferences in both the pattern and the replacement of sed substitutions.

Advanced sedIntermediate9 min readJul 10, 2026
Analogies

Capturing Parts of a Match

A group in a sed regular expression captures a portion of the match so you can refer to it later. In basic regular expressions (BRE, sed's default) groups are written with escaped parentheses '\(' and '\)'; with the -E or -r flag for extended regular expressions (ERE) you use plain '(' and ')'. Each group is numbered left to right by the position of its opening parenthesis, so the first group is 1, the second is 2, and so on, up to nine.

🏏

Cricket analogy: A group is like tagging a specific fielder's position on the field diagram so the commentator can reference 'the man at gully' again later without re-describing where he stands.

Backreferences in the Replacement

In the replacement text of an 's' command, '\1' through '\9' insert the text captured by the corresponding group, and '&' inserts the entire matched text. This lets you rearrange or reformat data: for example swapping 'Last, First' into 'First Last' by capturing each name and emitting them in reverse. The '&' is especially handy for wrapping a whole match, such as surrounding every matched number with brackets without retyping it.

🏏

Cricket analogy: Using '\1' and '\2' to swap names is like reversing the batting order on the scorecard — the same two players, just repositioned in the line-up.

Backreferences Inside the Pattern

Backreferences also work inside the pattern itself, where '\1' matches the exact text that group 1 already captured — not the pattern, but the literal string it matched. This is how you find repeated words or doubled characters: '\(\w\+\) \1' matches any word immediately repeated. Note this is a genuine regex feature enabling matches that plain regular languages cannot express, so it is powerful but can be slower on large inputs.

🏏

Cricket analogy: An in-pattern backreference is like spotting a bowler send down two identical deliveries back to back — you are matching the repeat of the very same ball, not just any ball.

Reformatting With Groups

bash
# Swap 'Last, First' into 'First Last' (BRE, default sed).
echo 'Curie, Marie' | sed 's/\(.*\), \(.*\)/\2 \1/'
# Output: Marie Curie

# Same task with extended regex (no backslashes on the parens).
echo 'Curie, Marie' | sed -E 's/(.*), (.*)/\2 \1/'

# Wrap every whole number in brackets using &.
echo 'order 42 line 7' | sed -E 's/[0-9]+/[&]/g'
# Output: order [42] line [7]

# Collapse doubled words using an in-pattern backreference.
echo 'the the cat' | sed -E 's/\b(\w+) \1\b/\1/g'
# Output: the cat

sed supports groups 1 through 9. If you need to reference a captured group but also want to keep the numbering simple, remember that groups are counted by the order of their opening parenthesis, left to right, even when groups are nested.

In basic regular expressions you MUST escape the parentheses as '\(' and '\)' and the plus as '\+'; in extended regex ('-E' or '-r') you must NOT escape them. Mixing the two conventions is the single most common sed regex bug — 's/(foo)/\1/' silently matches a literal parenthesis in BRE rather than creating a group.

  • Groups capture parts of a match: '\(...\)' in BRE, '(...)' in ERE (-E/-r).
  • Groups are numbered 1-9 by the position of their opening parenthesis.
  • In the replacement, '\1'-'\9' insert captured text and '&' inserts the whole match.
  • Backreferences in the pattern ('\1') match the exact text a group captured — great for finding repeats.
  • BRE requires escaping parentheses; ERE requires plain parentheses — do not mix them.
  • '&' is ideal for wrapping or duplicating an entire match without retyping it.
  • Backreference matching is powerful but can be slower on large inputs.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#BackreferencesAndGroups#Backreferences#Groups#Capturing#Parts#StudyNotes#SkillVeris#ExamPrep