我想使用正则表达式匹配单词 "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 匹配。有两种方法可以做到这一点。
如果您平台的 wchar_t 可以保存 Unicode 字符并且您平台的 C/C++ 运行时正确处理宽字符常量,则依赖 wchar_t。 (这有一些陷阱,不建议,阅读我提供的链接)
使用支持 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/