I have a list of strings and I want to create a regex that literally defines matches. I thought that it is enough just to list all valid strings separated with |
, as in the snippet below:
#include<iostream>
#include<regex>
int main(){
std::regex regex("one|two|three|four");
std::vector<std::string> candidates = {"one", "two", "three", "four", "five"};
for(auto it : candidates){
if(std::regex_match(it, regex)){
std::cout << it << " matches regex " << std::endl;
}
}
}
I expected to get following output:
$one matches regex
$two matches regex
$three matches regex
$four matches regex
Instead, only the two first candidates were a hit. I found out that it would work if I used parentheses, like this:
std::regex regex("((one|two)|three)|four");
But I would like to avoid it, because such a list can grow quite a lot.