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

Time:10-30

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?

CodePudding user response:

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

  • Related