gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:46:01 28 4
gpt4 key购买 nike

我想使用正则表达式匹配单词 "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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com