Byoungchan Lee
Byoungchan Lee

Reputation: 1502

Inconsistency between boost::regex and std::regex?

I'm coding with boost::regex and it seems like it doesn't give same result with C++11's std::regex.

Consider Following simple code:

#include <string>
#include <iostream>
#if defined(_MSC_VER) || (__cplusplus >= 201103L)
#include <regex>
#else // defined(_MSC_VER) || (__cplusplus >= 201103L)
#include <boost/regex.hpp>
#endif // defined(_MSC_VER) || (__cplusplus >= 201103L)

namespace {
#if defined(_MSC_VER) || (__cplusplus >= 201103L)
using std::regex;
using std::regex_replace;
#else // defined(_MSC_VER) || (__cplusplus >= 201103L)
using boost::regex;
using boost::regex_replace;
#endif // defined(_MSC_VER) || (__cplusplus >= 201103L)
} // namespace

int main() {
    std::string input = "abc.txt";
    ::regex re("[.]", ::regex::extended);

    std::string output = ::regex_replace(input, re, "\\.");

    std::cout << "Result : " << output << "\n";
    return 0;
}

C++11 Version (GCC 5.4, GCC 8.0 in wandbox, MSVC 2015) gives Result : abc\.txt

However, C++03 with Boost 1.64 version (GCC 8.0 in wandbox) gives Result : abc.txt

I also tried to ::regex::extended instead of ::regex::ECMAScript but they are same.

Is there are unknown MAGIC inconsistencies from boost::regex and std::regex?

Upvotes: 1

Views: 314

Answers (1)

He Ming
He Ming

Reputation: 88

I'm not sure it is deliberated or not. There is a boost::regex_constants::format_literal which can use as the fourth parameter for regex_replace, then you can get the same result as std::regex_replace. But there is no format_literal in standard c++ libraries.

Upvotes: 1

Related Questions