J.Bet-Eivazi
J.Bet-Eivazi

Reputation: 115

How to use Perl style memory regex matching with Boost library

I'm trying to use Perl's regex memory features, which puts matched text in () in variables $1, $2... Does anyone know how I can do this with Boost, maybe Boost saves the matched text in a different location? The following line of code says that $1 is undefined.

 boost::regex ex( aaa(b+)aaa, boost::regex::perl );
 if(boost::regex_search( line ,ex ))
   set_value( $1 ); // Here $1 should contain all the b's matched in the parenthesis

Thanks, Joe

Upvotes: 3

Views: 383

Answers (1)

Dave S
Dave S

Reputation: 21058

You will want to use a separate overload of boost::regex_search

In particular you want one where you pass in a boost::match_results structure (by reference). This will be populated with the sub-expressions (and the portion of the input that was matched), as long as the search is successful.

boost::match_results<std::string::const_iterator> results;
std::string line = ...; 
boost::regex ex( "aaa(b+)aaa", boost::regex::perl );
 if(boost::regex_search( line ,results, ex ))
   set_value( results[1] );

Upvotes: 3

Related Questions