14

私はこれらの変数を持っています:

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

関数が存在しないと表示されますが、現時点ではパラメーターが間違っていると思います。

4

1 に答える 1

36

基本的な使用例を次に示します。

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

出力

ハローオオオオオオオオ

こんにちはボブ

<boost/regex.hpp>boost_regex ライブラリを含め、リンクしていることを確認してください。

于 2012-07-04T02:33:31.190 に答える