【问题标题】:Setting Boost regex locale?设置 Boost 正则表达式语言环境?
【发布时间】:2012-04-23 05:50:20
【问题描述】:

在 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() 因为我总是希望设置美国语言环境。在不修改源代码的情况下如何做到这一点?

【问题讨论】:

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


    【解决方案1】:

    可以为每个正则表达式基础对象设置语言环境(检查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 object:

    #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
    • 不错!转向 xpressive 的另一个原因:)。太糟糕了,我需要使用旧的库。
    • 还有一个问题。编译lib之前需要设置BOOST_REGEX_USE_CPP_LOCALE吗?
    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2010-09-09
    • 2013-11-18
    • 2011-01-31
    • 2011-09-20
    相关资源
    最近更新 更多