With Perl, the following results in a match:
echo xyz | perl -ne 'print if (/.*(yes|no|xy).*/);'
I'm trying to achieve the same thing with a C++ regex. The ECMAScript syntax documentation says
A regular expression can contain multiple alternative patterns simply by separating them with the separator operator (|): The regular expression will match if any of the alternatives match, and as soon as one does.
However, the following example seems to suggest that std::regex_match only matches the first two alternatives, ignoring the third:
std::string pattern1 = ".*(yes|no|xy).*";
std::string pattern2 = ".*(yes|xy|no).*";
std::regex re1(pattern1);
std::regex re2(pattern2);
for (std::string str : {"yesplease", "bayes", "nobody", "anode", "xyz", "abc"} ) {
if (std::regex_match(str,re1)) {
std::cout << str << "\t\tmatches " << pattern1 << "\n";
}
else if (std::regex_match(str,re2)) {
std::cout << str << "\t\tmatches " << pattern2 << "\n";
}
}
Output:
yesplease matches .*(yes|no|xy).*
bayes matches .*(yes|no|xy).*
nobody matches .*(yes|no|xy).*
anode matches .*(yes|no|xy).*
xyz matches .*(yes|xy|no).*
How can I obtain the same behaviour as with my Perl regex example, i.e. having 'xyz' match pattern1?