我正在使用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::regex
和std::regex
是否存在未知的MAGIC不一致之处?
我不确定这是不是故意的。 有一个boost::regex_constants::format_literal
可以用作regex_replace
的第四个参数,然后你可以得到与 std::regex_replace 相同的结果。但是标准 c++ 库中没有format_literal
。