c++ - 帮助 boost::regex 修剪

标签 c++ boost boost-regex

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

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/

相关文章:

c++ - 如何在 C 代码中存储 C++ 对象?

c++ - 是什么导致了我的错误?我正在尝试创建一个玩家类并将玩家名称存储为变量

ios - 使用 Cmake for iOS 查找 boost 框架

c++ - 如何使用 AND 运算符编写正则表达式?

c++ - 跨多个发行版的 SDL

c++ - ‘type’ 中没有名为 ‘struct boost::enable_if<boost::is_pod<T>, void>’ 的类型

c++ - boost interprocess file_lock不适用于多个进程

c++ - Boost 跳过解析器是正确的方法吗?

c++ - 如何在 C++ 中停止 boost::regex_replace 的格式 ($)?

c++ - 如何在 C++ 的 boost::regex_replace() 中插入格式 str 并且不删除输入字符串中匹配的正则表达式?