Regex Capture Groups

Using Backreferences in Regex

How do you use backreferences in C++ regex?

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved