簡體   English   中英

我怎么知道boost :: regex_replace是否做了改變?

[英]how do I know if boost::regex_replace made a change?

在perl中,我可以做到這一點

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

在c ++中,我知道如何進行搜索/替換部分:

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

我怎么知道是否有更換?

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

也許有更好的方法可以做到這一點,但我在文檔中看不出任何暗示。 該函數似乎將輸入格式化和/或復制到輸出。 所以直截了當的解決方案是這樣的:

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()來確切地告訴您匹配的內容,然后自己替換子字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM