samedi 27 juin 2015

Why C++ regular expressions are broken in Visual Studio 2013?

It tried to start using regular expressions from STL <regex> header and found them totally broken in Visual Studio 2013.

It seems the only way to use them is to compile this sample: http://ift.tt/1iKe4cv

Anything outside the sample does not work.

Here is my piece of code with output results:

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

void test_regex(std::string str, std::string sRegex)
{
    std::string s(str);
    std::regex e(sRegex);
    std::smatch sm;
    std::regex_match(s, sm, e);
    std::cout << "regex_match(\"" << str << "\", \"" << sRegex << "\"): "
        << "found " << sm.size() << " matches\n";
}

int main()
{
    test_regex("test", ".*");
    test_regex("test", ".");
    test_regex("test\nline2", ".*");
    return 0;
}

Output:

regex_match("test", ".*"): found 1 matches
regex_match("test", "."): found 0 matches
regex_match("test
line2", ".*"): found 0 matches

First call shows that <regex> can give correct answer in some cases which are similar to sample from cplusplus.com.

Next two matches failed while they were still very very simple.

Aucun commentaire:

Enregistrer un commentaire