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.
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.
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.
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.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to regular expression capture groups, and how to use them in C++ with regex search
, replace
, iterator
, and token_iterator