std::regex_replace (Added in C++ 11) replaces all the occurrences. How can I make it replace only the first occurrence?
2 Answers
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.
3 Comments
Wiktor Stribiżew
I was writing it as I did not want to post without an example :( Here is the snippet.
Shah Rukh Qasim
regex_replace("231 is my string 1", regex("\\d+"), string("number"), std::tr1::regex_constants::format_first_only);
Micha Wiedenmann
@stribizhev Thank you, I feel sorry for you put in more effort.