how to use Boost regular expression replace method?

时间秒杀一切 提交于 2019-11-29 12:00:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/11321533/how-to-use-boost-regular-expression-replace-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!