正则表达式 - C++正则匹配中文乱码
迷茫
迷茫 2017-04-17 11:57:54
0
1
1446
#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++在匹配中文的时候,部分文字乱码,不知道大家遇到过这种情况吗

迷茫
迷茫

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

Antworte allen(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""可以解决

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!