Windows(VS2013 Update 4,VS2015 Update 3)上带有语法标志icase的:: std :: regex_replace与使用字符范围不匹配 [英] ::std::regex_replace with syntax flag icase on Windows (VS2013 Update 4, VS2015 Update 3) does not match using character ranges
问题描述
我在VS2013 Update 4和VS2015 Update 3中使用以下C ++代码,并使用字符范围来尝试不区分大小写地匹配并替换出现的内容:
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;
我期望输出:
Source: "Hallo Welt, HALLO WELT"
Result: "Hello Welt, Hello WELT"
但是我得到:
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?
推荐答案
我觉得这可能是Visual Studio中的错误。如果您从 [A]
中删除括号,则效果很好。
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);
如果您使用 regex_search
很奇怪找到2个匹配项...
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";
这篇关于Windows(VS2013 Update 4,VS2015 Update 3)上带有语法标志icase的:: std :: regex_replace与使用字符范围不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!