1

I'm using Boost.Regex to achieve something like this: search for a "|" and then take the left part of the "|" and put it a string, same with the right part:

string s1;
string s2;
who | sort

After this s1 should be "who" and s2 shoudl be "sort".
If I remember good, it was posible in Python, how can I do it using regular expressions in Boost ?

Thank you.

Marc
  • 3,683
  • 8
  • 34
  • 48
Adrian
  • 19,440
  • 34
  • 112
  • 219

2 Answers2

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

Split a string in C++?

Community
  • 1
  • 1
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
2

Here's short sample:

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

Additionally you maybe want to look on Boost.Tokenizer.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212