【问题标题】:how to use Boost regular expression replace method?如何使用 Boost 正则表达式替换方法?
【发布时间】:2012-07-04 12:17:06
【问题描述】:

我有这些变量:

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);

但它说该函数不存在,我猜现在参数不正确。

【问题讨论】:

    标签: c++ boost boost-regex


    【解决方案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;
    }
    

    输出

    喂喂喂喂

    你好鲍勃

    确保您包含 &lt;boost/regex.hpp&gt; 并已链接到 boost_regex 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      相关资源
      最近更新 更多