【问题标题】:std::regex_replace does not take fewer than 6 argumentsstd::regex_replace 不接受少于 6 个参数
【发布时间】: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++?
  • 我正在使用 libstdc++6。
  • g++ --version 打印什么? GCC 直到 version 4.9 才支持 C++11 正则表达式
  • 版本 4.8.1 ... 这解释了很多。 4.9 版似乎正在开发中,或者我可以将 gcc 更新到该版本吗?

标签: c++ regex linux c++11 std


【解决方案1】:

You need to update your compiler from GCC 4.8 to GCC 4.9.

不幸的是,GCC 4.9 尚未发布,但如果您迫切需要此功能,它足够稳定,可以与主干构建一起使用。

或者,试试 Clang 或 Boost.Regex

【讨论】:

  • boost.regex 也可能是一个可行的替代方案
  • @Cubbi:这是真的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 2021-08-14
  • 2022-11-24
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
相关资源
最近更新 更多