问题
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