Using Backreferences in Regex

How do you use backreferences in C++ regex?

Backreferences in regex allow you to refer to previously captured groups within the same pattern. This is useful for matching repeated substrings or enforcing certain constraints.

Basic Example

Here's a simple example where we use a backreference to match a word that repeats:

#include <iostream>
#include <regex>

int main() {
  std::string input = "hello hello world";
  std::regex pattern(R"((\w+)\s+\1)");
  std::smatch match;

  if (std::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str() << "\n";
    std::cout << "Repeated word: "
      << match[1].str();
  }
}
Match: hello hello
Repeated word: hello

In this example, (\w+) captures a word, and \1 refers to the same word captured by the first group. The pattern matches repeated words.

Using Multiple Backreferences

You can use multiple backreferences to enforce patterns with more complexity. For example, matching repeated sequences:

#include <iostream>
#include <regex>

int main() {
  std::string input = "abc abc def def";
  std::regex pattern(R"((\w+)\s+\1\s+(\w+)\s+\2)");
  std::smatch match;

  if (std::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str() << "\n";
    std::cout << "First repeated word: "
      << match[1].str() << "\n";
    std::cout << "Second repeated word: "
      << match[2].str() << "\n";
  }
}
Match: abc abc def def
First repeated word: abc
Second repeated word: def

Here, (\w+) captures a word and \1 refers to this word. The second capture group (\w+) captures another word, and \2 refers to this second word.

Summary

Backreferences in C++ regex are powerful for matching patterns where certain parts need to be repeated or constrained. By using \1, \2, etc., you can refer to previously captured groups, allowing for complex pattern matching and validation.

Regex Capture Groups

An introduction to regular expression capture groups, and how to use them in C++ with regex_search, regex_replace, regex_iterator, and regex_token_iterator

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Greedy vs Lazy Quantifiers
What are the differences between greedy and lazy quantifiers in regex?
Non-Capture Groups
What are non-capture groups and when should they be used?
Regex Replace in C++
How do you replace text in a string using regex in C++?
Splitting Strings with Regex
Can regex be used to split strings in C++?
Counting Regex Matches
How can you count the number of matches found in a string using regex?
Formatting Dates with Regex
How can you use regex to format dates in C++?
Lookahead and Lookbehind
What are lookahead and lookbehind assertions in regex?
Third-Party Regex Libraries
Are there any recommended third-party libraries for working with regex in C++?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant