I use this function to split the string:
std::vector<std::string> splitString(const std::string& stringToSplit, const std::string& regexPattern)
{
std::vector<std::string> result;
const std::regex rgx(regexPattern);
std::sregex_token_iterator iter(stringToSplit.begin(), stringToSplit.end(), rgx, -1);
for (std::sregex_token_iterator end; iter != end; ++iter)
{
result.push_back(iter->str());
}
return result;
}
Now, if I want to split a string line by line (say, I have read a file content into a single variable), I do this:
auto vec = splitString(fileContent, "\\n");
On Windows, I get this:
line 1 \r
line 2 \r
This happens because Windows line ending is determined with \r\n. I have tried to use $, but again without success. What is the right way to capture line endings in Windows, too?
[\\r\\n]+.auto vec = splitString(fileContent, "[\\r\\n]+");\rand\nare line separators, I think it will run on all OSes.