<分区>
我正在尝试让我的代码在 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 上没有产生预期的结果。