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

旧时模样 提交于 2019-12-11 06:57:52

问题


I use the following C++ code with VS2013 Update 4 and VS2015 Update 3 using a character range to try to match case insensitively and to replace the occurrences:

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;

I expected the output:

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

But I get:

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

Why the character range didn't get applied caseinsensitive? Why the second match didn't seem to be found and to be replaced?


回答1:


I feel like this might be a bug in Visual Studio. If you remove the brackets from [A] it works fine.

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

Oddly enough if you use a regex_search it finds 2 matches...

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";


来源:https://stackoverflow.com/questions/37026965/stdregex-replace-with-syntax-flag-icase-on-windows-vs2013-update-4-vs2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!