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?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
l33t
  • 18,692
  • 16
  • 103
  • 180

1 Answers1

3

It is possible to set the locale per regex basis object (check this for any gotchas):

boost::regex re;
re.imbue(std::locale("es_ES.UTF-8")); // or whatever you want
re.assign("[a-z]*"); // Important - assign after imbue!

Also there is a way to do it with 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 );
Anonymous
  • 18,162
  • 2
  • 41
  • 64
  • Ok, so if I define this macro, how do I set the locale for a specific regex object? – l33t Apr 10 '12 at 22:22
  • You can do it globally per application. I do not now how to solve your problem with a _smaller hammer_. – Anonymous Apr 10 '12 at 22:23
  • Alright. So how do you set the locale of the regex object? :P – l33t Apr 10 '12 at 22:27
  • Nice! Another reason to move to xpressive :). Too bad I need to use the older library. – l33t Apr 10 '12 at 22:45
  • 1
    One more question. Does `BOOST_REGEX_USE_CPP_LOCALE` need to be set before compiling the lib? – l33t Apr 10 '12 at 22:50
  • @NOPslider : See also [`BOOST_REGEX_NO_W32`](http://www.boost.org/libs/regex/doc/html/boost_regex/configuration/locale.html), which sounds more applicable to your specific question. – ildjarn Apr 10 '12 at 22:54
  • @NOPslider: yes, it affects the whole lib. But you shouldn't worry about that, as even on Windows _you can call `imbue` on the `basic_regex` object to set it's locale to some other `LCID` if you wish._ – Anonymous Apr 10 '12 at 23:09