简体   繁体   English

std::regex_replace 仅第一次出现

[英]std::regex_replace only first occurence

std::regex_replace (Added in C++ 11) replaces all the occurrences. std::regex_replace (在 C++ 11 中添加)替换所有出现的。 How can I make it replace only the first occurrence?我怎样才能让它只替换第一次出现?

If the flags parameter to std::regex_replace contains std::regex_constants::format_first_only , only the first match is replaced.如果std::regex_replaceflags参数包含std::regex_constants::format_first_only ,则仅替换第一个匹配项。

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

stribizhev kindly provided a working example . stribizhev好心地提供了一个工作示例

I found the solution myself.我自己找到了解决方案。 Posting for others if they face the same issue.如果他们面临同样的问题,则为其他人发帖。 Add:添加:

std::tr1::regex_constants::format_first_only

to replace only the first occurrence as the fourth argument to regex_replace仅将第一次出现替换为regex_replace的第四个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM