C# Regex Backreference

Summary: in this tutorial, you will learn about C# regex backreference to reference back to a capturing group within the same regular expression.

Introduction to the C# Regex Backreference

Backreference allows you to reference back to a captured group within the same regular expression. Backrefence is useful for matching repeated occurrences of a pattern.

To specify a backreference, you use \n where n represents the number of a captured group that you want to reference. Note that the first captured group is \1 , the second capture group is \2, and so on.

The following shows an example of using a backreference that matched a duplicate word in a sentence:

(\w+)\s+\1Code language: C# (cs)

For example, this pattern will match the words “the the” in the sentence:

“it’s the the best”

Here’s the explanation of the pattern:

  • (\w+): captures one or more word characters and stores them in the first capturing group.
  • \s+: matches one or more whitespace characters
  • \1: refers to the text captured by the first capturing group.

The following program returns the duplicate word “the” in the sentence “it’s the the best”:

using System.Text.RegularExpressions;
using static System.Console;


var text = "It's the the best";
var pattern = @"(\w+)\s+\1";

var matches = Regex.Matches(text, pattern);

foreach (var match in matches)
{
    WriteLine(match);
}Code language: C# (cs)

Output:

the theCode language: C# (cs)

The following program removes the repeated word using the backreference:

using System.Text.RegularExpressions;
using static System.Console;


var text = "It's the the best";
var pattern = @"(\b\w+)\s+\b\1\b";


var result = Regex.Replace(text, pattern, "$1");

WriteLine(result);Code language: C# (cs)

Output:

It's the bestCode language: C# (cs)

Notice that we use the Replace() method to replace the pattern with the “$1” in the text. The “$1” specifies the replacement pattern, which is the captured word ($1).

Regex backreferences are useful in some situations, for example:

  • Finding duplicate words or phrases in sentences (already demonstrated earlier).
  • Matching repeated patterns.
  • Ensuring symmetry in string structures.
  • Validating input based on previous matches.

Summary

  • Use a backreference to reference back to a captured group within the same regular expression.
Was this tutorial helpful ?