vendredi 29 octobre 2021

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?

Aucun commentaire:

Enregistrer un commentaire