正则表达式 - C++正则匹配中文乱码
迷茫
迷茫 2017-04-17 11:57:54
0
1
1444
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
 using namespace  std;
 void main(){
     string str = "今天是个好日子圣达菲阿斯qweer";
     regex pattern("[\u4e00-\u9fa5]");
     sregex_token_iterator end;  //需要注意一下这里
     for (sregex_token_iterator j(str.begin(), str.end(), pattern); j != end; ++j){
         cout << *j;
     }
     system("pause");
 }

C++在匹配中文的时候,部分文字乱码,不知道大家遇到过这种情况吗

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(1)
左手右手慢动作

u4e00-u9fa5 是符合Unicode的漢字
C++對unicode支援不太好,如果你是windows下的vs編譯的程序,普通字串編譯之後都是ANSI編碼也就是GBK,L""字串則是UTF16 LE,在c++11之後,可以嘗試使用u8""(UTF8) u""(UTF16)U""(UTF32)來指定unicode字串的不同UTF編碼形式

看原始碼regex應該是C++標準函式庫裡面的,在stackoverflow上找問題,一般反應是c++標準函式庫裡面的regex函式庫對unicode的支援並不好,
http://stackoverflow.com/questions /11254232/do-c11-regular-expressions...
http://stackoverflow.com/questions/15882991/range-of-utf-8-characters-...
http://stackoverflow. com/questions/17103925/how-well-is-unicode-suppor...

我不知道使用UTF32或UTF16能不能解決問題,一般建議的方法是boost::regex + icu
這個例子看起來用u""可以解決

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!