Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do backreferences match text captured earlier in a regex?

Quick answer

A numbered backreference such as \1 means “match the same text that capture group 1 matched earlier.” For example, \b(\w+)\s+\1\b can match go go: group 1 captures go, and \1 matches go again. In Python and JavaScript, numbered groups are counted by their opening parentheses. A backreference in a pattern is different from referring to a capture in replacement text.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does a backreference do? I want to find a character or word repeated twice.

Geez

A backreference points to text already captured by a numbered group, and asks the pattern to match that same text again. It does not mean “match any character” or “match any word” a second time: the second occurrence must be identical to what the group captured. Python and JavaScript both use \1 to refer to the first capturing group inside a pattern. [S1] [S2]

For two adjacent copies of one character, this pattern is a small example:

(.)\1

The dot captures one character in group 1; \1 then requires that character again. In llama, it can match ll or mm. To find a repeated word separated by whitespace, use:

\b(\w+)\s+\1\b

Here, group 1 captures a word, \s+ matches the whitespace between copies, and \1 requires the captured word again. The word boundaries help avoid matching just part of a longer word. [S1] [S2]

Follow-up question

Can you walk through what the repeated-word example captures and matches?

Geez

Suppose the text is We said go go now. With \b(\w+)\s+\1\b, the first word that can satisfy the pattern is the first go. The parentheses capture go as group 1. The whitespace part matches the space after it, and \1 matches the next go because it is the same captured text. The final boundary ends the match after that second word.

So the overall match is go go, while the captured value in group 1 is just go. Those are different things: the full match includes both copies and the space; the capture is the text stored by the parentheses. The same idea works for a repeated character with (.)\1: if the input has ee, group 1 captures the first e and the backreference matches the second. [S1] [S2]

A backreference checks equality with the captured text; it does not check whether the repeated word is sensible, or whether the text is a duplicate in any broader meaning.

Follow-up question

Is the syntax different in Python and JavaScript, and what should I type in each?

Geez

The numbered backreference inside the pattern is the same in these examples: \1 refers to the first capturing group, with groups numbered in the order their opening parentheses appear. [S1] [S2]

In Python, use a raw string so Python’s string parser does not interpret the backslashes first:

pattern = r"\b(\w+)\s+\1\b"

In JavaScript, the corresponding regular-expression literal is:

/\b(\w+)\s+\1\b/

For either pattern, applying it to We said go go now. can produce the match go go, with go in capture group 1. This is the pattern syntax; how you ask an application to search text or retrieve the match depends on the surrounding code and API. The cited documentation supports the numbered pattern-backreference examples, not every behavior of every regex feature or programming environment.

Follow-up question

How is that different from putting a captured value into replacement text?

Geez

A pattern backreference controls what text qualifies as a match. A replacement reference uses a captured value after a match has been found. They are separate operations, even though both refer to a capture.

For example, the pattern above finds the repeated phrase go go; it does not itself change the input. In Python, re.sub() replacement text can refer to captures using forms such as \g<1> for group 1 or \g<name> for a named group. [S1] Do not copy the pattern’s \1 into replacement text and assume it has the same role: the replacement format is a separate part of the operation. JavaScript also has its own replacement syntax and API; the cited backreference documentation establishes the pattern behavior, not the full replacement rules. [S2]

When debugging, first check that the pattern captures the text you expect and that its backreference matches the repeated text. Then check the replacement operation separately. If a pattern fails, inspect the actual input, including spaces and punctuation, and test a short example before using it on larger text.

A practical checklist

  • Put parentheses around exactly the text you want to capture.

  • Count capturing groups by the order their opening parentheses appear; \1 refers to the first.

  • Use a backreference in the pattern only when the later text must equal the captured text.

  • For Python patterns, consider a raw string such as r"\b(\w+)\s+\1\b".

  • Test against a short input and check the full match separately from the captured group.

  • Treat replacement references as a separate syntax from pattern backreferences.

A few more useful answers

Does `\1` match any text that looks similar to the capture?

No. It requires the same captured text again, rather than a new arbitrary character or word. For example, if group 1 captured go, the backreference requires go.

What is the difference between the full match and group 1?

The full match is all the text matched by the pattern. In go go, group 1 is the first go, while the full match includes both copies and the separating space.

Can I use a pattern backreference to replace the captured text?

A pattern backreference checks the match; it does not perform a replacement. Replacement text uses its own capture-reference syntax, such as Python’s \g<1> in re.sub().

Sources

References used for this guide.

  1. [S1] Python Regular Expression HOWTO: numbered backreferences
    docs.python.org · Checked
  2. [S2] MDN: JavaScript numbered backreferences
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work