boost::wregex如何支持中文啊

flamkuavos 2012-10-24 10:11:03
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
using std::wstring;
#include <locale>

#include "boost/tr1/regex.hpp"
using namespace boost;

void match_s(string sToMatch)
{
regex rg("(//w*)");
smatch sm;
if(regex_match( sToMatch, sm, rg ))
{
cout << "string 匹配结果:" << sm[1].str() << endl;
}
}

void match_ws(wstring wsToMatch)
{
wregex wrg(L"(//w*)");
wsmatch wsm;
if(regex_match( wsToMatch, wsm, wrg ))
{
int iLen= wcstombs( NULL, wsm[1].str().c_str(), 0 );
char *lpsz= new char[iLen+1];
int i= wcstombs( lpsz, wsm[1].str().c_str(), iLen );
lpsz[iLen] = '/0';
string sToMatch(lpsz);
delete []lpsz;
cout << "wstring 匹配结果:" << sToMatch << endl;
}
}

void main()
{
string sToMatch("数超限");
match_s( sToMatch );

sToMatch = "节点数目超限";
match_s( sToMatch );

setlocale( LC_CTYPE, "" );
int iWLen= mbstowcs( NULL, sToMatch.c_str(), sToMatch.length() );
wchar_t *lpwsz= new wchar_t[iWLen+1];
int i= mbstowcs( lpwsz, sToMatch.c_str(), sToMatch.length() );
wstring wsToMatch(lpwsz);
delete []lpwsz;
match_ws( wsToMatch );
}


winxp + vc6
这个是我在网络示例上改的,我写的函数不方便大家分析,这个示例完整可编译方便大家看,就是boost::regex_match总是返回false,头大啊,各位童鞋谁弄成功过boost::regex支持中文的啊,给说一下关键,最好是示例,多谢!

这boost::regex总这么难伺候的话,就要改用其他的更好的正则库了!头大!
...全文
426 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
flamkuavos 2012-10-25
  • 打赏
  • 举报
回复
1、明白了,这个是为了保证mbstowcs工作正常
2、html是很大一段啦
3、喔
4、呵呵

原来问题的关键不仅仅是缺少setlocale(LC_ALL,"chs");,还在于它的位置,太感谢二位了!
zjs100901 2012-10-25
  • 打赏
  • 举报
回复
#include <iostream>
using std::cout;
using std::endl;
#include <locale>

#include "boost/tr1/regex.hpp"
using namespace boost;

int main()
{
setlocale(LC_ALL,"chs");
char src_mb[] = "<title>123这里是标题</title>";
int i = mbstowcs( NULL, src_mb, strlen(src_mb) );
wchar_t *src_wc= new wchar_t[i + 1];
i = mbstowcs( src_wc, src_mb, strlen(src_mb) );
wregex regex_wc(L"<title>(.*)</title>");
wcmatch what_wc;
if(boost::regex_match( src_wc, what_wc, regex_wc ))
{
i = wcstombs( NULL, what_wc.str(1).c_str(), 0 );
char *rst_mb = new char[i + 1];
i = wcstombs( rst_mb, what_wc.str(1).c_str(), i );
rst_mb[i] = 0;
cout << "匹配结果:" << rst_mb << endl;
delete []rst_mb;
}
delete []src_wc;
return 0;
}

1. setlocale(LC_ALL,"chs");
2.
3.
4. boost西void main
flamkuavos 2012-10-25
  • 打赏
  • 举报
回复
喔喔喔,谢谢!我试了,貌似还没找出问题,现在我重写个了demo,一个html提取标题的功能,因为当中可能包含中文的,所以用boost的宽字节匹配,但总是不对,完整例子如下,请大侠有空编译了试一试喔

#include <iostream>
using std::cout;
using std::endl;
#include <locale>

#include "boost/tr1/regex.hpp"
using namespace boost;

void main()
{
char src_mb[] = " <title>123这里是标题</title> ";
int i = mbstowcs( NULL, src_mb, strlen(src_mb) );
wchar_t *src_wc= new wchar_t[i + 1];
i = mbstowcs( src_wc, src_mb, strlen(src_mb) );
wregex regex_wc(L"<title>.*</title>");
wcmatch what_wc;
setlocale(LC_ALL,"chs"); //不晓得是不是必须的
if(boost::regex_match( src_wc, what_wc, regex_wc ))
{
i = wcstombs( NULL, what_wc.str(1).c_str(), 0 );
char *rst_mb = new char[i + 1];
i = wcstombs( rst_mb, what_wc.str(1).c_str(), i );
rst_mb[i] = 0;
cout << "匹配结果:" << rst_mb << endl;
delete []rst_mb;
}
delete []src_wc;
}
赵4老师 2012-10-24
  • 打赏
  • 举报
回复
setlocale(LC_ALL,"chs");

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧