c++ - unicode 正则表达式 C++

标签 c++ regex boost unicode boost-regex

我想使用正则表达式匹配单词 "février" 或任何其他月份。

正则表达式:

^(JANVIER|FEVRIER|MARS|AVRIL|MAI|JUIN|JUILLET|AOUT|SEPTEMBRE|OCTOBRE|NOVEMBRE|DECEMBRE|Jan|Feb|Mar|Apr|May|Jun|JUN|Jul|Aug|Sep|Oct|Nov|Dec|[jJ]anvier|[Ff]évrier|[mM]ars|[aA]vril|[mM]ai|[jJ]uin|[jJ]uillet|[aA]o[éû]t|aout|[sS]eptembre|[oO]ctobre|[nN]ovembre|[dD][eé]cembre)$


问题

问题是我无法匹配包含 unicode 字母的单词:à,é,è 等。 我在以下网站上找到:Unicode é 的 unicode 值是 \u00E9。我可以将这个值整合到正则表达式中吗?以及如何在正则表达式中使用 unicode 值。


void returnValue(string pattern)
{
    bool x = false;
    const boost::regex e("février");
    x = boost::regex_search(pattern.c_str(),e);
    if(x){ cout <<"found"<<endl; }
}

最佳答案

您可以将 unicode 与 boost::regex 匹配。有两种方法可以做到这一点。

  1. 如果您平台的 wchar_t 可以保存 Unicode 字符并且您平台的 C/C++ 运行时正确处理宽字符常量,则依赖 wchar_t。 (这有一些陷阱,不建议,阅读我提供的链接)

  2. 使用支持 Unicode 的正则表达式类型 (boost::u32regex)。必须配置 Boost 以通过 Building With Unicode and ICU Support 启用此功能

http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/boost_regex/unicode.html

关于c++ - unicode 正则表达式 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932970/

相关文章:

c++ - 为什么我从 std::fmod 和 std::remainder 得到不同的结果

c# - C++ 和 C# 之间不一致的伪随机数

regex - 如何使用正则表达式验证带有可选百分比符号的小数?

php - 组合两个正则表达式函数来剥离 html 标签

php - 使用 preg_replace 将绝对 url 转换为相对 url

c++ - 使用 runge_kutta4 的高维数组

c++ - 如何创建枚举的模板类型

c++ - 在处理信号之前终止发出排队信号的线程是否安全?

android - Android 上 Boost 的链接错误

c++ - 使用boost计算CRC-5