在 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() 因为我总是希望设置美国语言环境。如何在不修改源代码的情况下做到这一点?
最佳答案
可以为每个正则表达式基础对象设置语言环境(检查 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 来实现:
#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 );
关于c++ - 设置 Boost 正则表达式语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10097218/