1

此正则表达式将在换行符处修剪字符串。我希望它只
修剪两端并保留中间的任何换行符。

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

1 回答 1

2

如果要使正则表达式在输入字符串的开头和结尾匹配(想要在中间保留空格\n), \A\z不是^and$可能会达到目的。
例如:

boost::regex expr("\\A[ \t]+|[ \t]+\\z");
于 2011-05-19T20:08:01.163 回答