locale对std::regex是有影响的
//从我的新浪博客搬过来
C++11 regex对locale是有依赖的。
如果使用的中文的话,很容易抛出bad_alloc异常!
默认情况是 locale是C,以下正则正常
const std::regex r1("^(\\s+)?(.*?)(\\s+)?=(\\s+)?(.*?)(\\s+)?");
std::cout << "std::regex r1: OK" << std::endl;
但改在中文语言就抛出bad_alloc异常了:
char* pLocale = setlocale(LC_ALL, "chs"); //bad
//char* pLocale = setlocale(LC_ALL, "C"); //good
std::cout << "setlocale(): " << pLocale << std::endl;
const std::regex r1("^(\\s+)?(.*?)(\\s+)?=(\\s+)?(.*?)(\\s+)?");
std::cout << "std::regex r1: OK" << std::endl;