1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know that such options matters! Thanks @He Ming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.