vc如何正则匹配出多条相同类似记录-[regex]

因为我对这个cmathresult模版类不懂,所以我的方法就是,每找到一个匹配项,那就把他替换为空,这样循环下去,直到得不到正则匹配项为止。

//just for google
bool CURLCollectorDlg::GetMatchCString2(CString strSearch, CString strBegin, CString strEnd ,CStringArray &arr)
{
	CString strReturn;
	//MessageBox("str" + strSearch);
	std::string str(strSearch.GetBuffer());
	//正则规则
	std::string strbegin,strend,strreg;
	strbegin = strBegin.GetBuffer();
	strend = strEnd.GetBuffer();
	strreg = "(.*?)";
	strreg = strbegin + strreg + strend;
	std::regex reg(strreg);
	//flag
	std::regex_constants::match_flag_type f = std::regex_constants::match_any;
	//存放匹配出来字符串
	std::match_results mr;

	if (std::regex_search(str,mr,reg,f))
	{
		AddStatus("匹配成功");
	} 
	else
	{
		AddStatus("匹配失败");
		return false;
	}
        //这个while循环是核心代码
	while(std::regex_search(str,mr,reg,f))
	{
		std::string s(mr.str());
		strReturn = s.c_str();
		//开始替换
		CString newstr;
		newstr = str.c_str();
		newstr.Replace(strReturn,"");
		str = newstr.GetBuffer(0);
		//
		strReturn.Replace(strBegin,"");
		strReturn.Replace(strEnd,"");
		strReturn.Replace("","");
		strReturn.Replace("","");
		//AddOneURL("匹配到的网址:" + strReturn);


		//str = str.replace(s,"",);
		arr.Add(strReturn);



	}
	return false;
	
}

Leave a Reply