行内の「$」ペアで区切られたいくつかの文字列間でループしようとしています。すべてのマーカーが置き換えられた出力行を取得するために、各一致を特定の値に置き換えますが、2番目の一致でスタックしています'新しい置換値を連結する方法がわからない:
const boost::regex expression( "\\$[\\w]+\\$" );
string fileLine( "Mr $SURNAME$ from $LOCATION$" );
string outLine;
string::const_iterator begin = fileLine.begin();
string::const_iterator end = fileLine.end();
boost::match_results<string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while ( regex_search( begin, end, what, expression, flags ) ) {
actualValue = valuesMap[what[0]];
ostringstream t( ios::out | ios::binary );
ostream_iterator<char, char> oi( t );
boost::regex_replace( oi, begin, end, expression, actualValue,
boost::match_default | boost::format_first_only );
outLine.append( t.str() );
begin = what[0].second;
}
問題は outLine.append( t.str() ) にあります。これは、最初の一致の後、outLine が次の一致の前の文字の一部を既に保持しているため、連結が適切に行われないためです。