Non-Capture Groups

What are non-capture groups and when should they be used?

Non-capture groups in regex allow you to group parts of your pattern without saving the matched content. This can be useful for applying quantifiers or logical operations to parts of your regex without creating additional groups in the result.

Syntax

Non-capture groups are defined using (?: ...) instead of the regular capture group parentheses ( ... ). For example:

#include <iostream>
#include <regex>

int main() {
  std::string input = "apple orange banana";
  std::regex pattern("(?:apple|orange) banana");
  std::smatch match;
  std::regex_search(input, match, pattern);
  std::cout << match.str();
}
orange banana

Here, (?:apple|orange) banana matches either "apple banana" or "orange banana", but does not create a separate capture group for "apple" or "orange".

Use Case 1: To Group Without Capturing

Use non-capture groups when you need to group parts of your regex pattern without the overhead of capturing. This is useful for cleaner and more efficient regex patterns.

Use Case 2: To Apply Quantifiers

Non-capture groups are helpful when you want to apply quantifiers to a group of expressions without capturing the group:

#include <iostream>
#include <regex>

int main() {
  std::string input = "The quick brown fox";
  std::regex pattern("(?:quick|slow) brown");
  std::smatch match;
  std::regex_search(input, match, pattern);
  std::cout << match.str();
}
quick brown

Use Case 3: To Control Alternation

They are also used to control alternation (|) without capturing the alternatives:

#include <iostream>
#include <regex>

int main() {
  std::string input = "red fox";
  std::regex pattern("(?:red|blue) fox");
  std::smatch match;
  std::regex_search(input, match, pattern);
  std::cout << match.str();
}
red fox

Non-capture groups provide flexibility in crafting regex patterns that are both efficient and easy to read.

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?
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?
Using Backreferences in Regex
How do you use backreferences in C++ 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