vc++6/MFC中使用boost示例,利用正则表达式分解字符串,替换文本等
vc++6/MFC中使用boost示例,利用正则表达式分解字符串,替换文本,批量替换等,void CMfcboostDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str,strValue,strTmp;
CStringArray arrayItem;
// vector<CString> items;
strValue=_T("汉字fdsafds233-2322322323汉字f23230923902390 13563718990 0537-82346788-555 fds0537-2222222-2222 13809099999");
strTmp=strValue;
//const char* szStr = "fdsafds233-2322322323f23230923902390 13563718990 0537-82346788-555 fds0537-2222222-2222 13809099999";
char* szStr=strValue.GetBuffer(strValue.GetLength());
//使用迭代器找出所有数字
boost::regex reg( "\\d{6,}|(1\\d{10})|(0\\d{2,3}-?|\\(0\\d{2,3}\\))?\\d{4,7}(-\\d{1,8})?" );
//boost::regex reg("(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}");//包括所有的电话及手机号码
boost::cregex_iterator itrBegin(szStr, szStr+strlen(szStr), reg);
boost::cregex_iterator itrEnd;
for(boost::cregex_iterator itr=itrBegin; itr!=itrEnd; ++itr)
{
// 指向子串对应首位置 指向子串对应尾位置 子串内容
// cout << (*itr).first-szStr << ' ' << (*itr).second-szStr << ' ' << *itr << endl;
int iStart=(*itr).first-szStr;
int iEnd=(*itr).second-szStr;
str=strValue.Mid(iStart,iEnd-iStart);
arrayItem.Add(str);
strTmp.Replace(str,_T(""));
// AfxMessageBox(str);
}
AfxMessageBox(strTmp);
//
str.Format(_T("%d\n"),arrayItem.GetSize());
for(int i=0;i<arrayItem.GetSize();i++)
{
str+=arrayItem.GetAt(i)+_T("\n");
}
AfxMessageBox(str);
}
static/image/hrline/1.gif
**** Hidden Message *****
提取码下载:
页:
[1]