此正则表达式将在换行符处修剪字符串。
我希望它仅 修剪两端并保留中间的任何换行符。
string s(" Stack \n Overflow ");
boost::regex expr("^[ \t]+|[ \t]+$");
std::string fmt("");
cout << boost::regex_replace(s, expr, fmt) << endl;
最佳答案
如果想让正则表达式匹配开头和
输入字符串的末尾(想要在 \n
之间保留空格),
\A
和 \z
而不是 ^
和 $
可能满足目的。
例如:
boost::regex expr("\\A[ \t]+|[ \t]+\\z");
关于c++ - 帮助 boost::regex 修剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6064057/