简体   繁体   中英

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

I'm trying to eliminate whitespaces in a string with a regular expression. From the following snippet:

std::string in = "abc>  <def\n"
                 "xyz>      \n";
std::regex re = R"((?:>)\s+(?:<)|\s+$)";
std::string out = std::regex_replace(in, re, "(newcontent)");

I'm expecting out to contain

abc>(newcontent)<def
xyz>(newcontent)

but, alas, I get

abc(newcontent)def
xyz>(newcontent)

instead. To me it seems that the non-capture sequence (?:...) isn't working. I've tried to modify the program with extensions as std::regex re ("....", std::regex::ECMAScript | std::regex::nosubs); and appending , std::regex_constants::format_default to regex_replace to no avail. Can this be a library implementation problem or am I at fault?

If the > and < are captured, they can be written back.
This way there are no assertion compatibility issues.

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

replace

$1(newcontent)$2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM