3

It seems that Boost.Locale cannot be used with std::regex. See the code below:

#include <regex>
#include <boost/locale.hpp>

int main()
{
    try {
        boost::locale::generator gen;
        std::locale de_DE(gen("de_DE.UTF-8"));
        std::wstring in(L"Grün GRÜN grün");

        std::wregex rx;
        rx.imbue(de_DE);
        rx.assign(L"grün", std::regex::icase);

        std::wcout << std::regex_replace(in, rx, L"green") << L"\n";
    }
    catch (std::exception &e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}

Outputs:

green GRÜN green

However this:

#include <regex>
#include <locale>

int main()
{
    try {
        std::locale de_DE("de_DE.UTF-8");
        std::wstring in(L"Grün GRÜN grün");

        std::wregex rx;
        rx.imbue(de_DE);
        rx.assign(L"grün", std::regex::icase);

        std::wcout << std::regex_replace(in, rx, L"green") << L"\n";
    }
    catch (std::exception &e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}

Correctly outputs:

green green green

Tested on Ubuntu 20.04.2. g++ 10.2, Boost.Locale built with ICU support using vcpkg (thus static link).

josuegomes
  • 451
  • 4
  • 15
  • I notice for me it prints `GR?N` (whereas chaning things from wchar_t to char it does print `GRÜN` correctly, albeit still not escaped. Maybe that's related? I know that C++ got u8/u16 literals, and perhaps that's relevant here? – sehe May 24 '21 at 20:39
  • @sehe not relevant. The issue is: std::regex, when using a locale from Boost.Locale, is not matching 'grün' with 'GRÜN' and thus not replacing it to 'green'. – josuegomes May 24 '21 at 21:51
  • I have noticed other issues in the interaction between `std::regex` and `boost:locale`, took me quite a while to realize that the issue was they don't play nice together. One workaround I found (don't know how feasible it would be in your case, and it seems a very frail workaround still) is to instantiate the `std::regex` before boost changes the locale. – Gregorio Litenstein Nov 04 '22 at 12:24

0 Answers0