OS X 和 Linux 上的 C++ (C++11) 正则表达式差异

标签 c++ regex c++11

<分区>

我正在尝试让我的代码在 OS X 和 Linux 上运行相同。 下面的代码是用 clang++ --std=c++11 regextest.cpp

编译的
#include <regex>
#include <iostream>
int main()
{
    std::string str = "/api/asd/";
    std::string pattern = "/api/(.*)/";
    std::cout << "Starting matching" << std::endl;
    std::smatch matches;
    if (std::regex_match(str, matches, std::regex(pattern, std::regex::egrep)))
    {
        std::cout << "Found match!" << std::endl;
        std::cout << "All matches: ";
        for (auto& it : matches)
            std::cout << it << ", ";
        std::cout << std::endl;
    }
    return 0;
}

在 OS X 上,运行这段代码的结果是:

Starting matching
Found match!
All matches: /api/asd/, asd,

另一方面,在 Linux 上(Gentoo、libstdc++ 3.3)

Starting matching
Found match!
All matches: /api/asd/, /asd/, //

它如何匹配 Linux 上的 /api/?为什么?

此外,尝试使用像 /api/([^/]) 这样的模式在 Linux 中完全失败并且不匹配但在 OS X 中运行良好。

我已经尝试过许多匹配类型的组合,(基本、扩展、grep、egrep、awk)与转义和未转义的 ()(取决于匹配类型)并且在 Linux 上没有产生预期的结果。

最佳答案

正如评论所建议的那样,通过将 gcc 升级到 4.9 解决了这个问题。 (目前在 Gentoo 上执行此操作需要 ~amd64 标志)。

关于OS X 和 Linux 上的 C++ (C++11) 正则表达式差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549573/

相关文章:

c++ - 为 Windows 部署 QT exe 时遇到问题

regex - 如何搜索一行中单词之间出现多个空格的情况

regex - 如何在Lua中写一个等价于/(\(\))?$/in js的正则表达式

arrays - 使用 lambda 捕获的 constexpr 值作为数组维度

c++ - 在其他线程QT/C++中调用方法

c++ - 在所有方向上移动矩阵列/行 C++

javascript - 正则表达式总是返回 true

c++ - 将成员函数指针的 vector 传递给函数

xcode - Osx Lion : Xcode 4. 1 如何设置 c++0x 项目

c++ - 如何用 CDockablePane 中的新 CMFCPropertyGridCtrl 替换