带 Boost 的 regex_search 和 regex_replace
我试图在一行中由“$”对分隔的一些字符串之间进行循环,用特定值替换每个匹配项,以便获得所有标记都被替换的输出行,但我陷入了第二个匹配项,因为我不这样做不知道如何连接新的替换值:
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 已经保存了下一个之前的一些字符匹配。
I'm trying to loop among some strings delimited by a '$' pair in a line, replacing each match with a specific value in order to get an output line with all markers replaced but I'm stuck at the second match as I don't know how to concatenate the new replacement value:
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;
}
The problem is in the outLine.append( t.str() ) as the concatenation is not done properly because after the first match, the outLine holds already some of the characters preceding the next match.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我不能 100% 确定你的意图,但我认为你的目标是取代
fileLine
中的每个匹配子字符串与valuesMap
的相应值。如果是这样,以下代码可能会满足您的目的:
希望这会有所帮助
Though I'm not 100% sure about your intent, I presume your goal is replacing
each matched substring in
fileLine
with the corresponding value ofvaluesMap
.If so, the following code might meet your purpose:
Hope this helps
由于您仅请求替换字符串中的第一个值(通过使用 boost::format_first_only 标志),原始字符串
将在第一次迭代时转换为
原始字符串,然后
将附加到它,因为您明确将 begin 设置为“什么[0].第二。
所以最终的输出
不是你需要的。
这是有副作用的工作示例 - 它修改 fileLine:
如果您不想修改 fileLine,那么您应该使用“begin”和“end”来标记仅包含一个模式的滑动窗口的开始和结束。
Since you request only first value in a string to be replaced (by using boost::format_first_only flag) original string
will be converted into
on first iteration and then
will be appended to it since you explicitly set begin to "what[0].second.
so final output is
which is not what you need.
Here is working example that has side effect - it modifies fileLine:
If you don't want to modify fileLine, then you should make "begin" and "end" to mark the beginning and end of sliding window that contains exactly one pattern.