std :: regex_replace只出现了第一次出现的C ++

时间:2015-10-13 14:39:36

标签: c++ regex c++11 visual-c++

std::regex_replace(在C ++ 11中添加)替换所有出现的内容。我怎样才能让它只替换第一次出现?

2 个答案:

答案 0 :(得分:10)

如果flags包含std::regex_constants::format_first_only,则只替换第一个匹配项。

std::regex_replace("12 34",
                   std::regex(R"(\d+)"),
                   "num",
                    std::regex_constants::format_first_only);

stribizhev提供了working example

<强>参考

答案 1 :(得分:0)

我自己找到了解决方案。如果他们遇到同样的问题,请发布给他人。 添加:

std::tr1::regex_constants::format_first_only

仅将第一个匹配项替换为regex_replace

的第四个参数