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?
It looks like regex is not fully implemented in gcc version 4.8.2 but rather in later versions of gcc (i.e., version > 4.9.0).
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53631
In gcc version 4.9.0 works ok LIVE DEMO
So I guess you'll have to upgrade to newer version of gcc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With