user754425
user754425

Reputation: 437

Help with boost::regex trim

This regex will trim the string at line breaks.
I want it to trim both end only and preserve any line breaks in the middle.

string s("     Stack \n Overflow    ");
boost::regex expr("^[ \t]+|[ \t]+$");
std::string fmt("");
cout << boost::regex_replace(s, expr, fmt) << endl;

Upvotes: 1

Views: 747

Answers (1)

Ise Wisteria
Ise Wisteria

Reputation: 11669

If you want to make the regular expression match at the beginning and the end of the input string(want to preserve spaces around the in-between \n), \A and \z instead of ^ and $ might meet the purpose.
For example:

boost::regex expr("\\A[ \t]+|[ \t]+\\z");

Upvotes: 2

Related Questions