3

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?

j_h
  • 103
  • 1
  • 7

1 Answers1

4

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.

101010
  • 41,839
  • 11
  • 94
  • 168
  • Yes, you're right. Just before you posted the answer I tried upgrading to 4.9.2, and now it seems to work. – j_h Jun 16 '15 at 20:19
  • It seems this issue was discussed here before, although that post didn't come up in any of my searches http://stackoverflow.com/a/12665408/3893211 – j_h Jun 16 '15 at 20:24