std::regex_replace
(在C ++ 11中添加)替换所有出现的内容。我怎样才能让它只替换第一次出现?
答案 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);
<强>参考强>
答案 1 :(得分:0)
我自己找到了解决方案。如果他们遇到同样的问题,请发布给他人。 添加:
std::tr1::regex_constants::format_first_only
仅将第一个匹配项替换为regex_replace