【问题标题】:std::regex_match with characters é è àstd::regex_match 与字符 é è à
【发布时间】:2020-04-20 15:14:44
【问题描述】:

例如,我想将 "ram" 、 "rém" 、 "rèm" 和 "ràm" 视为有效输入,所以我这样做:

std::string ss = "rém";
bool valid = std::regex_match(ss, std::regex("r[aéèà]m"));

但在这种情况下,'valid' 返回 false,字符 é、è 和 à 有什么特别之处吗?我应该修改正则表达式吗? 谢谢

【问题讨论】:

  • 可能是实现中的一个错误。你可以在 boost regex 上尝试同样的方法吗?
  • 使用的编码是什么? std::string不支持UTF...首选wstring
  • 在 VS2017 中运行此代码后,我得到了true
  • 这可能是个骗局,但我犹豫要不要敲它:stackoverflow.com/q/23932970/10077
  • 尝试声明std::wstring ss = L"rém",然后使用std::wcout << std::regex_match(ss, std::wregex(L"r[aéèà]m"));

标签: c++ regex c++11 visual-studio-2017 std


【解决方案1】:

您可以使用std::wstring 定义字符串,然后使用std::wregex 在Unicode 字符串上实际运行正则表达式:

std::wstring ss = L"rém";
std::wcout << std::regex_match(ss, std::wregex(L"r[aéèà]m"));
// => 1, there is a match

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 2016-11-13
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2013-07-20
    相关资源
    最近更新 更多