boost - Extract substring defined by start and end strings (C++) -


given std::wstring text, std::wstring start, std::wstring end, goal extract std::wstring text_out - containing text in between start , end - if exist in text.

note: if multiple matches start exist - consider first one, end - consider last match.

this works, there more elegant solution?

boost::iterator_range<wstring::iterator> it_start = boost::find_first( text, start); boost::iterator_range<wstring::iterator> it_end = boost::find_last( text, end);  if (it_start.empty() == false && it_end.empty() == false) {     text_out.assign( it_start.begin(), it_end.end() ); } else if ( it_start.empty() == false && it_end.empty() == true ) {     text_out.assign( it_start.begin(), text.end() ); } else if ( it_start.empty() == true && it_end.empty() == false ) {     text_out.assign( text.begin(), it_end.end() ); } 

i not see need use boost. same task can done using standard functions. example

#include <iostream> #include <string>   int main()  {     std::wstring text( l"startabcdefend" );     std::wstring start( l"start" );     std::wstring end( l"end" );      std::wstring::size_type pos = text.find( start );     pos = ( pos == std::wstring::npos ? 0 : pos + start.size() );      std::wstring::size_type n = text.rfind( end );      n = ( n == std::wstring::npos || n < pos ? std::wstring::npos : n - pos );      std::wcout << text.substr( pos, n ) << std::endl;      return 0; }  

the output is

abcdef 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -