samedi 24 décembre 2016

C++ std::regex_match does not match regex (works in other languages)

I'm trying to match unescaped versions of particular "tokens" using the c++ regular expressions. Some of the regular expressions generated by my code do not seem to work. I've narrowed down a test case:

Regular expression: (?:[^\\]|^)(?:\\\\)*(")

Expected effect: match a quote '"' character only if it is preceded by an even (possibly 0) number of escape characters '\', in case of the test string it should match the second quote character.

Test string: test \"string" test

When trying out the regular expression using https://regex101.com/ it works as expected.

Is there something wrong with my c++?

std::regex regex(R"DELIM((?:[^\\]|^)(?:\\\\)*("))DELIM");
std::smatch sm;
std::string subject=R"(test \"string" test)";
std::regex_match(subject,sm,regex);
std::cout<<"Matches: "<<sm.size()<<std::endl;

I am compiling with gcc gcc --version gives: gcc (Debian 4.9.2-10) 4.9.2

Aucun commentaire:

Enregistrer un commentaire