简体   繁体   中英

std::regex_replace only first occurence

std::regex_replace (Added in C++ 11) replaces all the occurrences. 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_replace("12 34",
                   std::regex(R"(\d+)"),
                   "num",
                    std::regex_constants::format_first_only);

stribizhev kindly provided a working example .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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