Regular Expressions › Backreferences

Michael BrenndoerferUpdated March 18, 20252 min read
Language AI HandbookAll chapters
Part 1 of 67Text as Data
Chapter 3Regular Expressions › Backreferences
Reading section 8 of 21 · Backreferences · 2 minSection 8 of 21 · 2 min

Book contents

Section 8 of 21 · 2 min read

Regular Expressions › BackreferencesOne section · Sections in this chapter8 / 21 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Backreferences let you match the same text that was captured by an earlier group. This is a feature that goes beyond simple pattern matching: it introduces a form of memory into the pattern. Without backreferences, regex can only describe the structure of what to match. With backreferences, it can express constraints across different positions in the string, specifically that two positions must contain the same text.

In[35]:
Code
# Find repeated words
text = "The the quick brown fox jumps over the the lazy dog dog"
pattern = r"\b(\w+)\s+\1\b"
repeated = re.findall(pattern, text, re.IGNORECASE)

# Find matching HTML tags
html = "<div>content</div> <span>text</span> <div>broken</span>"
pattern = r"<(\w+)>.*?</\1>"
valid_tags = re.findall(pattern, html)
Out[36]:
Console
Finding repeated words:
  Text: 'The the quick brown fox jumps over the the lazy dog dog'
  Pattern: \b(\w+)\s+\1\b
  Repeated words: ['The', 'the', 'dog']

Matching HTML tags:
  Text: '<div>content</div> <span>text</span> <div>broken</span>'
  Pattern: <(\w+)>.*?</\1>
  Valid tags: ['div', 'span']

The \1 refers back to whatever was captured by the first group. In the repeated words example, if the first group captures "the", then \1 only matches another "the", not any word.

Backreferences are powerful for validation tasks. The HTML tag example demonstrates a check that a simple <\w+>.*?</\w+> pattern cannot do: verifying that the closing tag matches the opening tag. This kind of constraint is impossible to express with pure character matching. In practice, backreferences are most useful for finding structural duplications in text, such as repeated words in proofreading tools or mismatched markup in document processing. Keep in mind that backreferences make the regex engine's job harder, because it must track and compare captured content at each position. Patterns with many backreferences can become slow on long documents.