c++ - boost::regex 和 std::regex 之间的不一致

标签 c++ regex boost c++11

<分区>

Possible Duplicate:
No matches with c++11 regex

我之前使用 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.*.*.*.*");
}

对我来说(gcc 4.7.2,-std=c++11,boost: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

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

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

该相信哪个?我猜 boost 在这里是正确的吗?

最佳答案

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

具体来说,boost.regex 支持 perl 扩展 listed here. ,但您没有使用其中任何一个。

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

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

Visual Studio 2012 的输出(无 boost )

Input   : [FOO.*]
Pattern : [FOO\.\*.*?]
std::regex_match: true
std::regex_search: true
Input   : [FOO.*.*.*.*]
Pattern : [FOO\.\*.*?]
std::regex_match: true
std::regex_search: true

仔细观察 clang 的差异,在第二次测试中,它匹配模式 [FOO\.\*.*?][FOO.*] 并离开 [.*.*.*] 不匹配,这很快归结为匹配 [S*?] 不同于 boost/visual studio.. 我认为这是一个错误也是。

关于c++ - boost::regex 和 std::regex 之间的不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13526884/

相关文章:

javascript - 正则表达式可选捕获未按预期工作

c++ - 如何迭代 Boost Multi_index 容器的索引?

c++ - 使用 VC++ 9 boost 警告

c# - 串行 COM 端口超时设置

c++ - 在 Visual C++ 中访问 MONITORINFOEX 值

Javascript 正则表达式 - 字符串到 RegEx 对象

php - 在php中使用preg_replace删除php中的空格

c++ - Boost::asio - 如何中断阻塞的 tcp 服务器线程?

c++ - 为什么这不会在接受输入后循环退出

c++ - ifstream::unget() 失败。 MS 的实现有问题还是我的代码有误?