在 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/