Greedy vs Lazy Quantifiers

What are the differences between greedy and lazy quantifiers in regex?

Greedy and lazy quantifiers control how much of the input string is matched by the quantifiers in a regex pattern.

Greedy Quantifiers

Greedy quantifiers match as much of the input string as possible. By default, the quantifiers *, +, and {n,m} are greedy. This means they try to match the longest possible substring that fits the pattern.

For example:

#include <iostream>
#include <regex>

int main() {
  std::string input = "abc123def456";
  std::regex pattern(".*\\d");
  std::smatch match;
  std::regex_search(input, match, pattern);
  std::cout << match.str();
}
abc123def456

In this example, .*\d matches the entire string because .* is greedy and matches as many characters as possible before the final digit.

Lazy Quantifiers

Lazy quantifiers match as little of the input string as possible. To make a quantifier lazy, you append a ? to it. The lazy versions are *?, +?, and {n,m}?.

For example:

#include <iostream>
#include <regex>

int main() {
  std::string input = "abc123def456";
  std::regex pattern(".*?\\d");
  std::smatch match;
  std::regex_search(input, match, pattern);
  std::cout << match.str();
}
abc1

Here, .*?\d matches only abc1 because .*? is lazy and stops matching as soon as it finds a digit.

When to Use Each

Use greedy quantifiers when you want to match the longest possible substring and lazy quantifiers when you want the shortest match.

Understanding the difference helps in fine-tuning regex patterns for specific use cases, such as extracting or replacing substrings within larger text.

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.

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?
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