I have these variables:

boost::regex re //regular expression to use std::string stringToChange //replace this string std::string newValue //new value that is going to replace the stringToChange depending on the regex. 

I only want to replace the first occurrence of it only.

Thanks fellas.

EDIT: I've found this:

boost::regex_replace(stringToChange, re, boost::format_first_only); 

but it says the function does not exists, I'm guessing the parameters are incorrect at the moment.

0

1 Answer

Here is an example of basic usage:

#include <iostream> #include <string> #include <boost/regex.hpp> int main(){ std::string str = "hellooooooooo"; std::string newtext = "o Bob"; boost::regex re("ooooooooo"); std::cout << str << std::endl; std::string result = boost::regex_replace(str, re, newtext); std::cout << result << std::endl; } 

Output

hellooooooooo

hello Bob

Make sure you are including <boost/regex.hpp> and have linked to the boost_regex library.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.