c++ (c++ 11)正则表达式在OS X和Linux上的差异
C++ (C++11) regular expressions differences on OS X and Linux
我试图让我的代码在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解决了。
相关文章:
- 在C++/Linux中设置单调时钟的一些技巧
- Linux的Cpp上的计时器
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux中哪里可以找到互斥、未来等的源代码
- 如何在linux终端中同时编译和运行c++代码
- 在Linux for Windows上编译C++代码时出错
- 如何找到锁定Linux futex的C++行
- 在linux上调试巨大的C++项目
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- c++方法参数只能在linux的发布模式下自行更改
- 在Linux和C++中的Windows上,散列字符串值会产生不同的输出
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?
- 函数在Windows或Linux上运行时表现不同
- 在Linux下捕获SIGKILL的C++程序
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- mysql C++ connector [mysql-connector-c++-8.0.19-linux-glibc2
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- CMake WxWidgets项目成功地在Linux上构建,但没有在Windows上构建