首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

boost:wregex怎么支持中文

2012-11-15 
boost::wregex如何支持中文啊C/C++ code#include iostreamusing std::coutusing std::endl#include s

boost::wregex如何支持中文啊

C/C++ code
#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总这么难伺候的话,就要改用其他的更好的正则库了!头大!

[解决办法]
setlocale(LC_ALL,"chs");
[解决办法]
C/C++ code
#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;} 

热点排行