Given a multiple line string A;1;\nB;2\nC;3
. It's needed to add a ;
before those newline
characters that are not immediately preceded by ;
. So, the result should be A;1;\nB;2;\nC;3
(a single ;
is added after 2
). I've tried different regex
es but without luck (one of them is presented below). I think the lookbehind assertion would do the job, but it isn't supported.
Question: What regex
expression can solve this problem? If none, how to solve it using other methods using pure C++ only (I'm using C++20
)?
std::string s{ "A;1;\nB;2\nC;3" };
const std::regex re("(?!;)\\n"); // Produces "A;1;;\nB;2;\nC;3" (redundant ";" after "1").
s = std::regex_replace(s, re, ";\n");