如何使用Boost正则表达式替换方法?

unw*_*guy 13 c++ boost boost-regex

我有这些变量:

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.
Run Code Online (Sandbox Code Playgroud)

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

谢谢费拉斯.

编辑:我发现了这个:

boost::regex_replace(stringToChange, re, boost::format_first_only);
Run Code Online (Sandbox Code Playgroud)

但它说功能不存在,我猜这些参数目前是不正确的.

Jes*_*ood 34

以下是基本用法示例:

#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;
}
Run Code Online (Sandbox Code Playgroud)

产量

hellooooooooo

你好鲍勃

确保包含<boost/regex.hpp>并链接到boost_regex库.