1

私はBoost.Regexを使用して、次のようなことを実現しています。「|」を検索する 次に、「|」の左側を取ります 右の部分と同じように、文字列を入れます。

string s1;
string s2;
who | sort

この後、s1は「who」になり、s2は「sort」になります。
よく覚えていれば、Pythonで可能でしたが、Boostで正規表現を使用してそれを行うにはどうすればよいですか?

ありがとうございました。

4

2 に答える 2

2
#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("|"));

文字列をC++で分割しますか?

于 2011-04-18T12:09:57.403 に答える
2

ここに短いサンプルがあります:

#include <iostream>
#include <boost/regex.hpp>

int main()
{
  // expression
  boost::regex exrp( "(.*)\\|(.*)" );
  boost::match_results<std::string::const_iterator> what;
  // input
  std::wstring input = "who | sort";
  // search
  if( regex_search( input,  what, exrp ) ) {
    // found
    std::string s1( what[1].first, what[1].second );
    std::string s2( what[2].first, what[2].second );
  }

  return 0;
}

さらに、 Boost.Tokenizerを調べたいと思うかもしれません。

于 2011-04-18T12:10:10.857 に答える