very basic regex scenario works different than my

2019-03-03 02:43发布

I'm getting a different behavior than my expectation (and also different than Microsoft C++).

Consider the following test.cpp file:

#include <iostream>
#include <ostream>
#include <regex>

int main( void )
{
    std::regex rx( "a(b+)(c+)d" );
    std::string s( "abbbbccd" );
    std::smatch m;

    bool f = regex_match( s, m, rx );
    std::cout << std::boolalpha << f << std::endl;
    if( f ) {
        std::cout << "m[1]=" << m[1] << std::endl;
        std::cout << "m[2]=" << m[2] << std::endl;
    }

    return 0;
}

On my Ubuntu Oneiric box, note how I compile the program, and note the output I'm getting from a.out:

$ g++ -std=c++0x test.cpp
$ ./a.out
true
m[1]=abbbb
m[2]=bcc

On the other hand, on my Windows machine, using Visual Studio 2010 I've:

C:> cl /EHsc test.cpp
C:> test.exe
true
m[1]=bbbb
m[2]=cc

I'm not an expert, but Microsoft Visual Studio seems to be the correct answer. This is a very basic scenario, so I wonder what is going on. I can't believe it's a bug, and I can't believe its a fundamental disagreement between MS and GNU at such a basic level. I suspect something in my configuration or in my command line. I got my g++ compiler and headers after installing the default Ubuntu 11.10 client, and 'apt-get install build-essentials'.

Could be a compilation switch that I'm missing, or a fundamental disagreement between MS and GNU

1条回答
Juvenile、少年°
2楼-- · 2019-03-03 03:16
登录 后发表回答