views:

576

answers:

1

How can I split a string with Boost with a regex AND have the delimiter included in the result list?

for example, if I have the string "1d2" and my regex is "[a-z]" I want the results in a vector with (1, d, 2)

I have:

std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
                                expression.end (), 
                                re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults));

Thanks

A: 

I think you cannot directly extract the delimiters using boost::regex. You can, however, extract the position where the regex is found in your string:

std::string expression = "1a234bc";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
for(; i!=j; ++i) {
  std::cout << (*i).position() << " : " << (*i) <<  std::endl;
}

This example would show:

1 : a

5 : b

6 : c

Using this information, you can extract the delimitiers from your original string:

std::string expression = "1a234bc43";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
size_t pos=0;
for(; i!=j;++i) {
  std::string pre_delimiter = expression.substr(pos, (*i).position()-pos); 
  std::cout << pre_delimiter << std::endl;
  std::cout << (*i) << std::endl;
  pos = (*i).position() + (*i).size();
}
std::string last_delimiter = expression.substr(pos);
std::cout << last_delimiter << std::endl;

This example would show:

1

a

234

b

c

43

There is an empty string betwen b and c because there is no delimiter.

J. Calleja