我試圖在一行中用'$'對分隔的一些字符串之間循環,用一個特定的值替換每個匹配,以便獲得所有標記替換的輸出行,但是我停留在第二場比賽,因爲我不知道如何連接新的替換值:regex_search and regex_replace with Boost
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拿着下一場比賽之前的一些角色。