设置Boost正则表达式语言环境?

时间:2012-04-10 22:12:55

标签: c++ regex boost locale boost-regex

在boost 1.48.0中,我在正则表达式代码中找到了这个(boost / regex / v4 / w32_regex_traits.hpp ):

w32_regex_traits()
      : m_pimpl(re_detail::create_w32_regex_traits<charT>(::boost::re_detail::w32_get_default_locale()))
   { }
//...//
BOOST_REGEX_DECL lcid_type BOOST_REGEX_CALL w32_get_default_locale()
{
    return ::GetUserDefaultLCID();
}

我需要覆盖这个w32_get_default_locale(),因为我总是希望设置美国语言环境。如何在不修改源代码的情况下完成这项工作?

1 个答案:

答案 0 :(得分:3)

可以为每个正则表达式基础对象设置区域设置(检查this是否存在任何问题):

boost::regex re;
re.imbue(std::locale("es_ES.UTF-8")); // or whatever you want
re.assign("[a-z]*"); // Important - assign after imbue!

还有一种方法可以使用Boost Xpressive per regex对象:

#include <locale>
#include <boost/xpressive/xpressive.hpp>
...
// Declare a regex_compiler that uses a custom std::locale
std::locale loc; /* ... create a locale here ... */;
boost::xpressive::regex_compiler<char const *, boost::xpressive::cpp_regex_traits<char> > cpprxcomp(loc);
boost::xpressive::cregex cpprx = cpprxcomp.compile( "\\w+" );

// or (after using boost::xpressive)
sregex cpprx2 = imbue(loc)( +_w );
相关问题