2015-10-13 47 views
0

std::regex_replace(在C++ 11中添加)取代所有的事件。我怎樣才能讓它只取代第一次出現?std :: regex_replace只有第一次出現C++

+0

歡迎StackOverflow上。我想指出http://stackoverflow.com/help/how-to-ask,特別是:「解釋你是如何遇到你想解決的問題的,以及任何阻礙你解決問題的困難你自己「。 –

回答

8

如果標誌包含std::regex_constants::format_first_only,則只替換第一個匹配項。

std::regex_replace("12 34", 
        std::regex(R"(\d+)"), 
        "num", 
        std::regex_constants::format_first_only); 

stribizhev友情提供了一個working example

參考

+0

用法示例會非常好:) – HRgiger

+0

我正在寫它,因爲我不想在沒有示例的情況下發布:(這是[片段](http://ideone.com/PvzQgf)。 –

+0

regex_replace(「231 is我的字符串1「,正則表達式(」\\ d +「),字符串(」數字「),std :: tr1 :: regex_constants :: format_first_only); –

0

我找到了自己的解決方案。如果他們面臨同樣的問題,則發佈給他人。 地址:

std::tr1::regex_constants::format_first_only

僅替換第一次出現的第四個參數regex_replace