我刚刚开始使用 boost 和 c++,我正在努力理解 boost 的正则表达式引擎在匹配空格时的行为。如果我使用代码:
boost::regex rx(" ");
cout << regex_search(" ", rx);
匹配空格,然后一切都按预期工作,regex_search 返回 true。但是,如果我尝试用“\s”替换正则表达式以匹配所有空白字符,我永远不会得到匹配,并且以下代码始终输出 false:
boost::regex rx("\\s");
cout << regex_search(" ", rx);
我在这里错过了什么?
根据要求,这是我的完整测试用例:
#include <boost/regex.hpp>
#include <iostream>
using namespace std;
int main()
{
boost::regex rx("\\s", boost::regex::icase);
cout << regex_search(" ", rx);
}
最佳答案
明白了——我最初使用的是来自 ascend4.org/Binary_installer_for_Boost_on_MinGW 的预建库。在构建 Boost 1.52 后,代码按预期工作。试图缩短 boost 构建过程最终让我损失了几个小时的挫败感……我现在已经吸取了教训!
关于c++ - boost 正则表达式不匹配 "\\s"到空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614171/