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

3
在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个回答

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 );

好的。那么你如何设置正则表达式对象的区域设置呢? :P - l33t
不错!又一个使用xpressive的理由 :)。可惜我需要使用旧的库。 - l33t
1
还有一个问题。在编译库之前,是否需要设置BOOST_REGEX_USE_CPP_LOCALE - l33t
@NOPslider:另请参阅BOOST_REGEX_NO_W32,这似乎更适用于您的具体问题。 - ildjarn
@NOPslider:是的,它会影响整个库。但是你不用担心,因为即使在Windows上,您也可以调用 imbuebasic_regex 对象上设置其区域设置为其他 LCID(如果您愿意). - Anonymous
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接