::std::regex_replace with syntax flag icase on Windows(VS201

::std::regex_replace with syntax flag icase on Windows (VS2013 Update 4, VS2015 Update 3) does not match using character ranges

本文关键字:icase on Windows VS201 flag syntax std regex replace with      更新时间:2023-10-16

我在VS2013更新4和VS2015更新3中使用以下C++代码,使用字符范围来尝试不区分大小写匹配并替换出现的内容:

std::wstring strSource(L"Hallo Welt, HALLO WELT");
std::wstring strReplace(L"ello");
std::regex_constants::syntax_option_type nReFlags =
    std::regex::ECMAScript |
    std::regex::optimize |
    std::regex::icase;
std::wregex  re(L"[A]LLO", nReFlags);
std::wstring strResult = std::regex_replace(strSource, re, strReplace);
wcout << L"Source: "" << strSource.c_str() << L""" << endl
      << L"Result: "" << strResult.c_str() << L""" << endl;

我期望输出:

Source: "Hallo Welt, HALLO WELT"
Result: "Hello Welt, Hello WELT"

但我得到了:

Source: "Hallo Welt, HALLO WELT"
Result: "Hello Welt, HALLO WELT"

为什么字符范围没有应用区分大小写?为什么第二场比赛似乎没有找到并被替换?

我觉得这可能是Visual Studio中的一个错误。如果将括号从[A]中移除,则效果良好。

std::wregex  re(L"ALLO", nReFlags);

奇怪的是,如果你使用regex_search,它会找到2个匹配项。。。

std::wregex  re(L"([A]LLO)", nReFlags);
std::wsmatch match;
std::regex_search(strSource, match, re);
for (auto i = 0; i < match.size(); ++i)
    std::wcout << match[i] << "n";