boost::regex 和std::regex之间的不一致?

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

本文关键字:regex 不一致 std boost 之间      更新时间:2023-10-16

我正在使用boost::regex编码,似乎它与 C++11 的std::regex没有给出相同的结果。

请考虑以下简单代码:

#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版本(GCC 5.4,wandbox中的GCC 8.0,MSVC 2015(提供了Result : abc.txt

但是,带有Boost 1.64版本的C++03(wandbox中的GCC 8.0(提供了Result : abc.txt

我也试图::regex::extended而不是::regex::ECMAScript但它们是一样的。

boost::regexstd::regex是否存在未知的MAGIC不一致之处?

我不确定这是不是故意的。 有一个boost::regex_constants::format_literal可以用作regex_replace的第四个参数,然后你可以得到与 std::regex_replace 相同的结果。但是标准 c++ 库中没有format_literal