cppreference中的regex_search抛出错误

时间:2014-02-04 01:16:53

标签: regex c++11

我在cppreference网站上运行the sample program并抛出错误。怎么可能?请帮忙

我也尝试使用我的4.8.1 GCC编译器,它也会抛出错误。

输出:

  

在抛出'std :: regex_error'

的实例后终止调用
  what():  regex_error
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string lines[] = {"Roses are #ff0000",
                           "violets are #0000ff",
                           "all of my base are belong to you"};

    std::regex color_regex("#([a-f0-9]{2})"
                            "([a-f0-9]{2})"
                            "([a-f0-9]{2})");

    for (const auto &line : lines) {
        std::cout << line << ": " 
                  << std::regex_search(line, color_regex) << '\n';
    }   

    std::smatch color_match;
    for (const auto &line : lines) {
        std::regex_search(line, color_match, color_regex);
        std::cout << "matches for '" << line << "'\n";
        for (size_t i = 0; i < color_match.size(); ++i) {
            std::ssub_match sub_match = color_match[i];
            std::string sub_match_str = sub_match.str();
            std::cout << i << ": " << sub_match_str << '\n';
        }   
    }   
}

1 个答案:

答案 0 :(得分:5)

在GCC 4.8.1中,完全支持C ++ 11核心语言,但与Clang不同,GCC并不完全支持C ++ 11标准库。 正如the libstdc++ Implementation Status page所示,std :: regex尚未完全实现。