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?

0

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

Shame on me, I just used Test parameters in incorrect order :(
No need for an SO question; it's described on the standard library support table for GCC (see '28, Regular expressions')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.