2014-02-28 50 views
1

我有以下一个简单的例子:其中<regex>库用于在“hello world”中搜索“llo”。是否<regex>库工作正常?

#include <regex> 
#include <iostream> 
#include <string> 
using namespace std; 
int main(void) 
{ 
    cout << boolalpha; 
    regex rgx("llo"); 
    cmatch result; 

    cout << regex_search("hello world", result, rgx) << endl; 
    cout << "Matched: " << result.str() << endl; 
    return 0; 
} 

我用 “gcc版本4.7.1” 编译它使用以下命令:

c++ regex2.cpp -o regex2 -std=c++11 

,其结果是:

false 
Matched: 

那么,什么是应该我做的错误?自编译以来,我可以假设c++11能够在我的电脑上,因此库也能吗?

衷心的感谢!

+0

不是。 。 。 。 –

回答

3

不,std::regex不是完全在GCC 4.7.1附带的标准库实现中实现。

全面支持<regex>将与GCC 4.9今年晚些时候发布。

看到这个:http://gcc.gnu.org/gcc-4.9/changes.html

在此期间,我建议使用<boost/regex.hpp>类似的语法。

#include <boost/regex.hpp> 

#include <iostream> 
#include <string> 

int main() { 
    std::cout << std::boolalpha; 
    boost::regex rgx("llo"); 
    boost::cmatch result; 

    std::cout << boost::regex_search("hello world", result, rgx) << std::endl; 
    std::cout << "Matched: " << result.str() << std::endl; 
}