C ++ Boost: Split String

How can I split a string using Boost with a regular expression AND include a delimiter in the list of results?

for example, if I have the string "1d2" and my regular expression is "[az]" 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)); 

thank

+3
source share
1 answer

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

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 shows:

1: a

5: b

6: c

Using this information, you can extract the delimiters from the 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 shows:

1

a

234

b

from

43

betwen b c, .

+5

Source: https://habr.com/ru/post/1725554/


All Articles