【发布时间】:2014-02-18 15:53:26
【问题描述】:
我正在尝试学习如何在 c++11 中使用正则表达式库。 在 ubuntu 13.10 上,我正在尝试从 cplusplus.com 编译以下示例:
// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main ()
{
std::string s ("there is a subsequence in the string\n");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
// using string/c-string (3) version:
std::cout << std::regex_replace (s,e,"sub-$2");
// using range/c-string (6) version:
std::string result;
std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
std::cout << result;
// with flags:
std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
std::cout << std::endl;
return 0;
}
使用命令:
$ g++ -std=c++11 -o file file.cc
我得到以下输出:
file.cc:13:48: error: no matching function for call to ‘regex_replace(std::string&, std::regex&, const char [7])’
我做错了什么?!我的头上没有多少头发了... 提前致谢
【问题讨论】:
-
您使用的是哪个版本的 gcc 和 libstdc++?
-
适用于 clang++/libc++ coliru.stacked-crooked.com/a/e52aacb4bb121c9c
-
我正在使用 libstdc++6。
-
g++ --version打印什么? GCC 直到 version 4.9 才支持 C++11 正则表达式 -
版本 4.8.1 ... 这解释了很多。 4.9 版似乎正在开发中,或者我可以将 gcc 更新到该版本吗?