2012-06-28 48 views
3

在Perl中,我能做到這一點我如何知道boost :: regex_replace是否進行了更改?

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

在C++中,我知道該怎麼做查找/替換部分:

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

我怎麼知道是否有替換的話?

我可以比較舊的價值和新的價值。有沒有更好的辦法?

回答

1

也許有這樣做的更好的方法,但我看不到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(),告訴你到底什麼是匹配的,然後自己替換子字符串。

+0

+1對於似乎不被「接受」的正確答案, – cppanda

相關問題