regex – 青春部落,流年似水 http://www.youthtribe.com 青春是一场远行,总记不起来时的路。 Wed, 15 Oct 2014 09:23:53 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.1.7 regex – 青春部落,流年似水 http://www.youthtribe.com/archives/1373 http://www.youthtribe.com/archives/1373#respond Wed, 15 Oct 2014 09:23:18 +0000 http://www.youthtribe.com/?p=1373 因为我对这个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;
	
}
]]>
http://www.youthtribe.com/archives/1373/feed 0