我在一个函数中用到正则筛选字符串,但是什么都没有筛到。函数是:
BOOL CEmailSendApp::GetSourceHtml(CString theUrl,CString Filename)
{
CInternetSession session;
CInternetFile* file = NULL;
try
{
// 试着连接到指定URL
file = (CInternetFile*) session.OpenURL(theUrl);
}
catch (CInternetException* m_pException)
{
// 如果有错误的话,置文件为空
file = NULL;
m_pException->Delete();
return FALSE;
} // 用dataStore来保存读取的网页文件
CStdioFile dataStore;
if (file)
{
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
//BOOL bIsOk = dataStore.Open(strPath+"\\"+Filename,
BOOL bIsOk = dataStore.Open(Filename,
CFile::modeCreate
| CFile::modeWrite
| CFile::shareDenyWrite
| CFile::typeText); if (!bIsOk)
return FALSE; string str; // 读写网页文件,直到为空
while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
{
dataStore.WriteString(somecode);
dataStore.WriteString(_T("\n")); //如果somecode采用LPTSTR类型,可不用此句 if(parse(somecode)!="")
{
str +=parse(somecode);
} } file->Close();
AfxMessageBox((LPCTSTR)str.c_str());
delete file;
}
else
{
dataStore.WriteString(_T("到指定服务器的连接建立失败..."));
return FALSE;
}
return TRUE;
}
string CEmailSendApp::parse(CString str)
{
std::string str1;
boost::smatch matches;
boost::regex expression("html");
ConvertCString2string(str, str1); if(regex_match(str1, matches, expression))
{
return matches.str();
}
else
{
return "Not found";
}
}void CEmailSendApp::ConvertCString2string(CString& strSrc,std::string& strDes)
{
#ifndef UNICODE
strDes = strSrc;
#else
USES_CONVERSION;
strDes = W2A(strSrc.LockBuffer());
strSrc.UnlockBuffer();
#endif
}请问以上代码哪里出错?
3X
BOOL CEmailSendApp::GetSourceHtml(CString theUrl,CString Filename)
{
CInternetSession session;
CInternetFile* file = NULL;
try
{
// 试着连接到指定URL
file = (CInternetFile*) session.OpenURL(theUrl);
}
catch (CInternetException* m_pException)
{
// 如果有错误的话,置文件为空
file = NULL;
m_pException->Delete();
return FALSE;
} // 用dataStore来保存读取的网页文件
CStdioFile dataStore;
if (file)
{
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
//BOOL bIsOk = dataStore.Open(strPath+"\\"+Filename,
BOOL bIsOk = dataStore.Open(Filename,
CFile::modeCreate
| CFile::modeWrite
| CFile::shareDenyWrite
| CFile::typeText); if (!bIsOk)
return FALSE; string str; // 读写网页文件,直到为空
while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
{
dataStore.WriteString(somecode);
dataStore.WriteString(_T("\n")); //如果somecode采用LPTSTR类型,可不用此句 if(parse(somecode)!="")
{
str +=parse(somecode);
} } file->Close();
AfxMessageBox((LPCTSTR)str.c_str());
delete file;
}
else
{
dataStore.WriteString(_T("到指定服务器的连接建立失败..."));
return FALSE;
}
return TRUE;
}
string CEmailSendApp::parse(CString str)
{
std::string str1;
boost::smatch matches;
boost::regex expression("html");
ConvertCString2string(str, str1); if(regex_match(str1, matches, expression))
{
return matches.str();
}
else
{
return "Not found";
}
}void CEmailSendApp::ConvertCString2string(CString& strSrc,std::string& strDes)
{
#ifndef UNICODE
strDes = strSrc;
#else
USES_CONVERSION;
strDes = W2A(strSrc.LockBuffer());
strSrc.UnlockBuffer();
#endif
}请问以上代码哪里出错?
3X
string CEmailSendApp::parse(CString str)
{
std::string str1, str2;
boost::regex expression("html");
boost::smatch matches; ConvertCString2string(str, str1); int new_counter=0;
int delete_counter=0;
boost::match_flag_type flags = boost::match_default;
std::string::const_iterator it=str1.begin();
std::string::const_iterator end=str1.end();
while (boost::regex_search(it,end,matches,expression, flags))
{ matches[1].matched ? ++new_counter : ++delete_counter;
it=matches[0].second;
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
str2 += matches.str();
str2 +="\n"; }
return str2;
}