1
#include <iostream>
#include <string>
#include <regex>

using namespace std;

void Test(const char* str, const char* regExpression)
{
    regex rx(regExpression);
    bool match = regex_match(str, rx);
    bool search = regex_search(str, rx);

    cout << "String: " << str << "  expression: " << regExpression <<
        "   match: " << (match ? "yes" : "no ") <<
        "   search: " << (search ? "yes" : "no ")  << endl;
}


int main()
{
    Test("a", "a");
    Test("a", "abc");
    return 0;
}

Results in g++:

String: a  expression: a   match: yes   search: no 
String: a  expression: abc   match: no    search: no 

Results in VS2012:

String: a  expression: a   match: yes   search: yes
String: a  expression: abc   match: no    search: no

What is correct result?. Also, what is the difference between regex_match and regex_search?

Alex F
  • 42,307
  • 41
  • 144
  • 212

1 Answers1

2

The VS2012 results are right. _match checks whether your string matches the expression, _search checks whether some substring of your string matches the expression.

Neither "a" nor any substring of "a" match the expression "abc".

(I can't find the relevant SO question, but gcc's (rather, libstdc++'s) regex implementation is known to be buggy and incomplete.)

us2012
  • 16,083
  • 3
  • 46
  • 62
  • Shame on me, I just used Test parameters in incorrect order :( – Alex F Mar 03 '13 at 12:15
  • 1
    No need for an SO question; it's described on the [standard library support table for GCC](http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x) (see '28, Regular expressions') – jogojapan Mar 03 '13 at 12:15
  • See http://stackoverflow.com/questions/12530406/is-gcc4-7-buggy-about-regular-expressions/12665408#12665408 and http://stackoverflow.com/a/4716974/981959 and loads more questions – Jonathan Wakely Mar 03 '13 at 16:45