boost :: regex和std :: regex之间不一致

Nim*_*Nim 4 c++ regex boost c++11

可能重复:
与c ++ 11正则表达式不匹配

boost::regex以前用过一些东西和一些我想要使用的新东西,std::regex直到我注意到以下不一致 - 所以问题是哪一个是正确的?

#include <iostream>
#include <regex>
#include <string>

#include <boost/regex.hpp>

void test(std::string prefix, std::string str)
{
  std::string pat = prefix + "\\.\\*.*?";

  std::cout << "Input   : [" << str << "]" << std::endl;
  std::cout << "Pattern : [" << pat << "]" << std::endl;

  {
    std::regex r(pat);
    if (std::regex_match(str, r))
      std::cout << "std::regex_match: true" << std::endl;
    else
      std::cout << "std::regex_match: false" << std::endl;

    if (std::regex_search(str, r))
      std::cout << "std::regex_search: true" << std::endl;
    else
      std::cout << "std::regex_search: false" << std::endl;
  }

  {
    boost::regex r(pat);
    if (boost::regex_match(str, r))
      std::cout << "boost::regex_match: true" << std::endl;
    else
      std::cout << "boost::regex_match: false" << std::endl;

    if (boost::regex_search(str, r))
      std::cout << "boost::regex_search: true" << std::endl;
    else
      std::cout << "boost::regex_search: false" << std::endl;
  }
}

int main(void)
{
  test("FOO", "FOO.*");
  test("FOO", "FOO.*.*.*.*");
}
Run Code Online (Sandbox Code Playgroud)

对我来说(gcc 4.7.2,-std = c ++ 11,提升:1.51),我看到以下内容:

Input   : [FOO.*]
Pattern : [FOO\.\*.*?]
std::regex_match: false
std::regex_search: false
boost::regex_match: true
boost::regex_search: true
Input   : [FOO.*.*.*.*]
Pattern : [FOO\.\*.*?]
std::regex_match: false
std::regex_search: false
boost::regex_match: true
boost::regex_search: true
Run Code Online (Sandbox Code Playgroud)

如果我将模式更改为贪婪模式(.*),那么我看到:

Input   : [FOO.*]
Pattern : [FOO\.\*.*]
std::regex_match: true
std::regex_search: false
boost::regex_match: true
boost::regex_search: true
Input   : [FOO.*.*.*.*]
Pattern : [FOO\.\*.*]
std::regex_match: true
std::regex_search: false
boost::regex_match: true
boost::regex_search: true
Run Code Online (Sandbox Code Playgroud)

哪一个相信?我猜这boost是正确的吗?

Cub*_*bbi 7

gcc当然不支持tr1/c ++ 11正则表达式,但是为了给出更一般的答案,根据其文档,boost.regex的默认值是perl 5,而C++默认是ECMAScript,由几个依赖于语言环境的元素扩展POSIX BRE.

具体来说,boost.regex支持此处列出的perl扩展.,但你没有使用任何这些.

现在,我很好奇并通过另外两个编译器运行测试:

clang的输出:

~ $ clang++ -o test test.cc -std=c++11 -I/usr/include/c++/v1 -lc++ -lboost_regex
~ $ ./test
Input   : [FOO.*]
Pattern : [FOO\.\*.*?]
std::regex_match: true
std::regex_search: true
boost::regex_match: true
boost::regex_search: true
Input   : [FOO.*.*.*.*]
Pattern : [FOO\.\*.*?]
std::regex_match: false
std::regex_search: true
boost::regex_match: true
boost::regex_search: true
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2012的输出(没有提升)

Input   : [FOO.*]
Pattern : [FOO\.\*.*?]
std::regex_match: true
std::regex_search: true
Input   : [FOO.*.*.*.*]
Pattern : [FOO\.\*.*?]
std::regex_match: true
std::regex_search: true
Run Code Online (Sandbox Code Playgroud)

看着哗哗的差异接近,在第二测试模式匹配[FOO\.\*.*?][FOO.*]离开[.*.*.*]无与伦比,迅速归结为匹配[S*?]从升压/ Visual Studio的不同.我想这,是一个错误了.