I need to remove the leading and trailing whitespace of a string. It is from the GetText()
from tinyxml2 which can have \t
and \n
characters in it which does not look good if I print out the text.
I have this line that as far as I understand is correct syntax for std::regex
. I have verified the regex expression with the online regex.
std::string Printer::trim(const std::string & str)
{
return std::regex_replace(str, std::regex("^\s+|\s+$"), "", std::regex_constants::format_default);
}
My understanding is that it will replace all of the leading and trailing white space with empty strings. Which effectively removes the trailing and preceding white space.
At result of passing in a test string \t\t\t\nhello world\t\t\n
gives back the string \t\t\t\nhello world\t\t\n
and the output should have been hello world
.
The additional question I have is does c++ use the exact same regex syntax as ECMAScript
? Also what kind of performance cost is associated with using regex over a more traditional approach such as using string.substr()
I am aware that there are other methods to achieving the same affect but I have plans to use std::regex
in other places of my project so I am hoping I can figure out how to use this instead.
Aucun commentaire:
Enregistrer un commentaire