say I have a text, represented as std::string, which contains several different newline, e.g. \r\n but also just \n or even just \r.
I would like now to unify this by replacing all non \r\n newlines, namely all \r and all \n newlines with \r\n.
A simple boost::replace_all(text, "\n", "\r\n"); doesn't work unfortunatly because that would also replace the \n within the already valid \r\n's.
I think std::regex should be a good way to handle this... but how should I express this in a regex? Here is some code:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string text = "a\rb\nc\r\nd\n";
std::regex reg(""); // What to put here?
text = std::regex_replace(text, reg, "\r\n");
std::cout << text;
}
The text should at the end just be "aaa\r\nbbb\r\nccc\r\nddd\r\n"