【问题标题】:::std::regex_replace with syntax flag icase on Windows (VS2013 Update 4, VS2015 Update 3) does not match using character ranges::std::regex_replace 与 Windows 上的语法标志 icase(VS2013 Update 4、VS2015 Update 3)不匹配使用字符范围
【发布时间】:2016-05-04 11:49:47
【问题描述】:

我将以下 C++ 代码与 VS2013 Update 4 和 VS2015 Update 3 一起使用,使用字符范围尝试不区分大小写地匹配并替换出现的位置:

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"

为什么没有应用字符范围不区分大小写? 为什么第二个匹配项似乎没有找到并被替换?

【问题讨论】:

    标签: c++ regex visual-studio-2013 stl case-insensitive


    【解决方案1】:

    我觉得这可能是 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";
    

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多