I'm trying to get my code to work on both OS X and Linux the same.
The code below is compiled with 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;
}
On OS X, the result of running this code is:
Starting matching
Found match!
All matches: /api/asd/, asd,
On Linux, on the other hand (Gentoo, libstdc++ 3.3)
Starting matching
Found match!
All matches: /api/asd/, /asd/, //
How does it match /api/
on Linux? Why?
Additionally, trying to use a pattern like /api/([^/])
fails completely in Linux and matches nothing but works well in OS X.
I've tried many combinations of match types, (basic, extended, grep, egrep, awk) with escaped and unescaped (
and )
(depending on the match type) and nothing produces the expected results on Linux.