如何使用非捕获组将标记之间的空格替换为 std::regex_replace?

How do I replace spaces with std::regex_replace between markers using non-capture groups?

我正在尝试使用正则表达式消除字符串中的空格。来自以下片段:

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 无济于事。这可能是图书馆实施问题还是我的错?

如果><被捕获,可以写回
这样就没有断言兼容性问题。

(>)\s+(<)|\s+$

替换

(newcontent)

https://regex101.com/r/dOjUlw/1