Combine Multiple Regex
How do I combine multiple regex patterns into one in C++?
Combining multiple regex patterns into one in C++ can be useful when you need to match any one of several patterns. Here are some common ways to achieve this:
Using the Alternation Operator (|
)
The most straightforward way to combine multiple regex patterns is to use the alternation operator |
, which matches any one of the separated patterns.
For example, suppose you want to match either "cat" or "dog":
#include <iostream>
#include <regex>
int main() {
std::string text{"I have a cat and a dog"};
std::regex pattern{"cat|dog"};
auto words_begin = std::sregex_iterator(
text.begin(), text.end(), pattern);
auto words_end = std::sregex_iterator();
for (
std::sregex_iterator i = words_begin;
i != words_end;
++i
) {
std::smatch match = *i;
std::cout << "Match: "
<< match.str() << "\n";
}
}
Match: cat
Match: dog
Using Non-Capturing Groups
If you want to combine more complex patterns and avoid capturing groups in your matches, you can use non-capturing groups (?: ...)
to group patterns without affecting the match results.
Below, we match either "cat", "dog", or "fish" without capturing the groups:
#include <iostream>
#include <regex>
int main() {
std::string text{
"I have a cat, a dog, and a fish"};
std::regex pattern{"(?:cat|dog|fish)"};
auto words_begin = std::sregex_iterator(
text.begin(), text.end(), pattern
);
auto words_end = std::sregex_iterator();
for (
std::sregex_iterator i = words_begin;
i != words_end;
++i
) {
std::smatch match = *i;
std::cout << "Match: "
<< match.str() << "\n";
}
}
Match: cat
Match: dog
Match: fish
Combining Patterns with Common Prefixes
If the patterns share common prefixes, you can optimize by combining them more efficiently. For instance, to match "apple", "apricot", or "apartment", you can use a combined pattern:
#include <iostream>
#include <regex>
int main() {
std::string text{
"I like apples, apricots, and apartments"
};
std::regex pattern{"ap(?:ple|ricot|artment)"};
auto words_begin = std::sregex_iterator(
text.begin(), text.end(), pattern);
auto words_end = std::sregex_iterator();
for (
std::sregex_iterator i = words_begin;
i != words_end;
++i
) {
std::smatch match = *i;
std::cout << "Match: "
<< match.str() << "\n";
}
}
Match: apples
Match: apricots
Match: apartments
Using Raw String Literals for Complex Patterns
For complex patterns that involve a lot of special characters, using raw string literals can make the regex more readable:
#include <iostream>
#include <regex>
int main() {
std::string text{
"I have a cat, a dog, and a fish"
};
std::regex pattern{
R"((cat|dog|fish))" // Raw string literal
};
auto words_begin = std::sregex_iterator(
text.begin(), text.end(), pattern);
auto words_end = std::sregex_iterator();
for (
std::sregex_iterator i = words_begin;
i != words_end;
++i
) {
std::smatch match = *i;
std::cout << "Match: "
<< match.str() << "\n";
}
}
Match: cat
Match: dog
Match: fish
By using these techniques, you can combine multiple regex patterns into one, making your pattern matching more efficient and easier to manage.
Regular Expressions
An introduction to regular expressions, and how to use them in C++ with std::regex
, std::regex_match
, and std::regex_search