windows平台下,C++正则表达式匹配中文失败
windows 正则表达式对Unicode的支持不好,首先需要设置系统的本地还环境,然后在需要使用宽字符来处理。测试代码如下
#include "stdafx.h"
#include <regex>//正则表达式库为regex,使用C++11及以上即可正常使用
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char * pre = setlocale(LC_ALL, "");
cout << pre << endl;
char *now = setlocale(LC_ALL, "chs");
cout << now << endl;
wchar_t ch[] = L"^[a-zA-Z0-9\u4e00-\u9fa5_]{2,8}$";
wstring parten(ch);
wregex re(parten);
wstring str;
while (wcin >> str)
{
if (regex_match(str, re)) cout << "success" << endl;
else cout << "fail" << endl;
}
setlocale(LC_ALL, pre);
char *repre = setlocale(LC_ALL, "");
cout << repre << endl;
return 0;
}