带 Boost 的 regex_search 和 regex_replace

发布于 2024-10-22 05:51:16 字数 925 浏览 2 评论 0原文

我试图在一行中由“$”对分隔的一些字符串之间进行循环,用特定值替换每个匹配项,以便获得所有标记都被替换的输出行,但我陷入了第二个匹配项,因为我不这样做不知道如何连接新的替换值:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-10-29 05:51:16

虽然我不能 100% 确定你的意图,但我认为你的目标是取代
fileLine 中的每个匹配子字符串与 valuesMap 的相应值。
如果是这样,以下代码可能会满足您的目的:

  ...same as your code...

  while ( regex_search( begin, end, what, expression, flags ) ) {
    outLine.insert( outLine.end(), begin, what[0].first );
    outLine += valuesMap[what[0]];
    begin = what[0].second;
  }

  outLine.insert( outLine.end(), begin, end );

希望这会有所帮助

Though I'm not 100% sure about your intent, I presume your goal is replacing
each matched substring in fileLine with the corresponding value of valuesMap.
If so, the following code might meet your purpose:

  ...same as your code...

  while ( regex_search( begin, end, what, expression, flags ) ) {
    outLine.insert( outLine.end(), begin, what[0].first );
    outLine += valuesMap[what[0]];
    begin = what[0].second;
  }

  outLine.insert( outLine.end(), begin, end );

Hope this helps

玩世 2024-10-29 05:51:16

由于您仅请求替换字符串中的第一个值(通过使用 boost::format_first_only 标志),原始字符串

"Mr $SURNAME$ from $LOCATION$"

将在第一次迭代时转换为

"Mr ACTUAL_VAL from $LOCATION$"

原始字符串,然后

" from ACTUAL_VAL"

将附加到它,因为您明确将 begin 设置为“什么[0].第二。
所以最终的输出

"Mr ACTUAL_VAL from $LOCATION$ from ACTUAL_VAL"

不是你需要的。
这是有副作用的工作示例 - 它修改 fileLine:

   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 ) ) 
    {
        const char* actualValue = valuesMap[what[0]];

        ostringstream t( ios::out | ios::binary );
        ostream_iterator<char, char> oi( t );

        boost::regex_replace( oi, begin, end, expression, 
`enter code here`actualValue, boost::match_default | boost::format_first_only );

        fileLine.assign(t.str());
        begin = fileLine.begin();
        end = fileLine.end();        
    }

    std::cout << fileLine << std::endl;

如果您不想修改 fileLine,那么您应该使用“begin”和“end”来标记仅包含一个模式的滑动窗口的开始和结束。

Since you request only first value in a string to be replaced (by using boost::format_first_only flag) original string

"Mr $SURNAME$ from $LOCATION$"

will be converted into

"Mr ACTUAL_VAL from $LOCATION$"

on first iteration and then

" from ACTUAL_VAL"

will be appended to it since you explicitly set begin to "what[0].second.
so final output is

"Mr ACTUAL_VAL from $LOCATION$ from ACTUAL_VAL"

which is not what you need.
Here is working example that has side effect - it modifies fileLine:

   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 ) ) 
    {
        const char* actualValue = valuesMap[what[0]];

        ostringstream t( ios::out | ios::binary );
        ostream_iterator<char, char> oi( t );

        boost::regex_replace( oi, begin, end, expression, 
`enter code here`actualValue, boost::match_default | boost::format_first_only );

        fileLine.assign(t.str());
        begin = fileLine.begin();
        end = fileLine.end();        
    }

    std::cout << fileLine << std::endl;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文