2013-07-12 43 views
0

我正在使用C++正則表達式。無法掌握以下編程輸出。C++ regex_match行爲

#include <iostream> 
#include <regex> 
#include <algorithm> 
#include <string> 
using namespace std; 

int main(){ 
    regex r("a(b+)(c+)d");  
    string s ="abcd"; 
    smatch m; 
    cout << s << endl; 
    const bool b = regex_match(s,m, r); 
    cout << b <<endl; // prints 1 - OK 
    if(b){ 
    cout << m[0] << endl; // prints abcd - OK 
    cout << m[1] << endl; // prints ab - Why? Should it be just b? 
    cout<< m[2] << endl; // prints bc - Why? Should it be just c? 
    } 

} 

我接觸到其他語言的正則表達式,括號應該匹配字符串的捕獲部分?所以輸出應該是

1 
abcd 
b 
c 

編輯: 我使用G ++ 4.6

+0

無法重現。如預期的那樣,我的VC2010副本打印出'b'和'c'。 –

+0

我可以重現使用g ++ 4.7 – dreamlax

+0

@Igor我在這裏使用g ++ 4.6。 – David

回答