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

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

最佳答案

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

相关文章:

正则表达式查找配置文件中的所有子网 block

java正则表达式: group count fixed

java - 如何将java正则表达式转换为mysql正则表达式

c++ - 使用 boost 多精度库的问题

c++ - 为什么未指定 std::bind 的返回类型?

c++ - 显式运算符 << 选择 'wrong' 重载

c++ - 用我自己的方法扩展 Protobuf

c++ - 制作邻接表,奇怪的错误?

c++ - 以某种方式缓冲或包装 cin,以便我可以使用 tellg/seekg?

c++ - Boost.asio 和异步链,unique_ptr?