如何使用boost正则表达式替换的方法?(how to use Boost regular expr

2019-06-26 00:32发布

我有这些变量:

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.

我只想只替换了它的第一次出现。

感谢小伙子们。

编辑:我发现这一点:

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

但它说的功能不存在,我猜的参数是此刻不正确。

Answer 1:

这是基本用法的例子:

#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;
}

产量

hellooooooooo

你好鲍勃

请确保您包括<boost/regex.hpp>并已链接到boost_regex库。



文章来源: how to use Boost regular expression replace method?