gpt4 book ai didi

c++ - boost::regex 和 std::regex 之间不一致?

转载 作者:行者123 更新时间:2023-11-30 05:11:21 24 4
gpt4 key购买 nike

我正在使用 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,GCC 8.0 in wandbox,MSVC 2015)给出 Result : abc\.txt

但是,带有 Boost 1.64 版本 ( GCC 8.0 in wandbox ) 的 C++03 给出 Result : abc.txt

我也尝试使用 ::regex::extended 而不是 ::regex::ECMAScript 但它们是相同的。

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

最佳答案

我不确定它是否经过深思熟虑。有一个boost::regex_constants::format_literal可以作为regex_replace的第四个参数,那么你可以得到和std::regex_replace一样的结果。但是标准 c++ 库中没有 format_literal

关于c++ - boost::regex 和 std::regex 之间不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45093572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com