#include <iostream>
#include <regex>
int main() {
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, "$1;$2");
std::cout << s;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmVnZXg+CgppbnQgbWFpbigpIHsKCXN0ZDo6c3RyaW5nIHN7ICJBOzE7XG5COzJcbkM7MyIgfTsKCWNvbnN0IHN0ZDo6cmVnZXggcmUoIihbXjtdfF4pKFxcbikiKTsgIC8vIFByb2R1Y2VzICJBOzE7O1xuQjsyO1xuQzszIiAocmVkdW5kYW50ICI7IiBhZnRlciAiMSIpLgoJcyA9IHN0ZDo6cmVnZXhfcmVwbGFjZShzLCByZSwgIiQxOyQyIik7CglzdGQ6OmNvdXQgPDwgczsKCXJldHVybiAwOwp9