提升正则表达式不匹配“\\ s”到空格

时间:2012-11-28 20:53:01

标签: c++ regex boost whitespace boost-regex

我刚刚开始使用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);
}

1 个答案:

答案 0 :(得分:2)

知道了 - 我最初使用的是来自ascend4.org/Binary_installer_for_Boost_on_MinGW的预建库。在构建Boost 1.52之后,代码按预期工作。试图快速推进构建过程最终导致我花了几个小时的挫折......我现在已经吸取了教训!