我正在嘗試使用正則運算式消除字串中的空格。從以下片段:
std::string in = "abc> <def\n"
"xyz> \n";
std::regex re = R"((?:>)\s (?:<)|\s $)";
std::string out = std::regex_replace(in, re, "(newcontent)");
我期待out包含
abc>(newcontent)<def
xyz>(newcontent)
但是,唉,我明白了
abc(newcontent)def
xyz>(newcontent)
反而。對我來說,非捕獲序列似乎(?:...)不起作用。我試著擴展,修改后的方案std::regex re ("....", std::regex::ECMAScript | std::regex::nosubs);,并追加, std::regex_constants::format_default到regex_replace無濟于事。這可能是庫實作問題還是我有錯?
uj5u.com熱心網友回復:
如果>和<被捕獲,它們可以被寫回。
這樣就沒有斷言兼容性問題。
(>)\s (<)|\s $
代替
$1(newcontent)$2
https://regex101.com/r/dOjUlw/1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341552.html
下一篇:回傳base64字串的檔案型別
