Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ regex replace first match

I tried to write a method which acts like Java's Matcher::replaceFirst(String replacement). I know how to replace all matches in a std::string, with std::regex_replace, but how can I only replace the first match of a regex in a string?

like image 715
Exagon Avatar asked Nov 01 '25 12:11

Exagon


1 Answers

You can use std::regex_replace with an additional flag, std::regex_constants::format_first_only, to get the result you want.

Take a look at http://en.cppreference.com/w/cpp/regex/regex_replace for additional information.

like image 95
Moreira Avatar answered Nov 03 '25 05:11

Moreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!