如何使用升压定期EX pression替代方法? [英] how to use Boost regular expression replace method?

查看:122
本文介绍了如何使用升压定期EX pression替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些变量:

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.

感谢小伙子们。

编辑:我发现这一点:

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.

推荐答案

下面是基本用法的例子:

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

输出

hellooooooooo

hellooooooooo

您好鲍勃

请确保您包括&LT;升压/ regex.hpp方式&gt; ,并已链接到boost_regex库

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

这篇关于如何使用升压定期EX pression替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆