3

In boost 1.48.0 I find this in the regex code (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();
}

I need to override this w32_get_default_locale() since I always want US locale to be set. How can this be done without modifying the source code?

4

1 回答 1

3

可以为每个正则表达式基础对象设置语言环境(检查这个是否有任何陷阱):

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 来做到这一点:

#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 );
于 2012-04-10T22:21:17.610 回答