64,637
社区成员
发帖
与我相关
我的任务
分享
#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 );
}
#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;
}
#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;
}