c++ - 我怎么知道 boost::regex_replace 是否进行了更改?

标签 c++ boost

在 perl 中,我可以做到这一点

if($str =~ s/a/b/) {
  do something
}

在 C++ 中,我知道如何进行搜索/替换部分:

str = boost::regex_replace(str, boost::regex("a"), "b",  
                           boost::match_default | boost::format_perl ) ;

我怎么知道是否进行了任何替换?

我可以将旧值与新值进行比较。有没有更好的办法?

最佳答案

也许有更好的方法,但我在 documentation 中看不到任何提示.该函数似乎格式化和/或将输入复制到输出。所以直接的解决方案是这样的:

std::string result = boost::regex_replace(str, boost::regex("a"), "b",
                                          boost::match_default | boost::format_perl);
if (result != str) {
    // Do something with "result".
}

但是,如果您觉得需要一个非常高效的实现,您可以使用 regex_match()这会准确告诉您匹配的内容,然后您自己替换子字符串。

关于c++ - 我怎么知道 boost::regex_replace 是否进行了更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11252502/

相关文章:

c++ - 与小代码的结果相比,boost 的字节序检查不同

c++ - 插入 unordered_map 时没有匹配函数

c++ - 使用 Boost::Spirit 解析具有未知键的 'key = value' 列表

c++ - 向列表中添加一个新元素,并返回对该元素的引用?

c++ - 编译器将 cout'ing 抛出循环

c++ - 在堆数据结构中环绕问题

c++ - 如何在 C++ 中实现缓存?

C++ boost::posix_time::ptime 默认值

c++ - 画一个立方体和一个金字塔

c++ - 图中的路径数