Beispiel #1
0
// 仅用来搜索关键字,结果保存在char* resultFile中
void SearchKeysInFile(const char* file,const char * keys,const char * resultFile)
{
	// text
	//char * text = "12.5, a1.1, 0.123, 178";

	// declare
	//static CRegexpT <char> regexp("\\b\\d+\\.\\d+");
	//char * key=m_Keyword.GetBuffer();
	static CRegexpT <char> regexp(keys);
	CContext * pContext;

	//读文件,分块查找
	std::ifstream ifs;
	ifs.open(file,ifstream::binary);
	ifs.seekg(0,ifstream::beg);

	std::ofstream ofs;
	ofs.open(resultFile,ofstream::binary);
	ofs.clear();
	//ofs.seekg(0,ofstream::beg);

	char * pBuffer=new char[BLOCK_SIZE];
	while(!ifs.eof())
	{
		ifs.read(pBuffer,BLOCK_SIZE);

		//////////////////////////////////////////////////////////////////////////
		//分块查找

		// prepare
		pContext= regexp.PrepareMatch(pBuffer);

		// loop
		MatchResult result = regexp.Match(pContext);

		while( result.IsMatched() )
		{
			// 写入结果文件
			//	printf("%.*s\n", result.GetEnd() - result.GetStart(), pBuffer + result.GetStart());
			
			// 先转换,再写入
			ofs.write(pBuffer + result.GetStart(),result.GetEnd() - result.GetStart());
			ofs<<endl;
			ofs<<"——————————————————————"<<endl;
			// get next
			result = regexp.Match(pContext);
		}
	}

	// 搜索成功结束
	ifs.close();
	ofs.close();
	

	// release
	regexp.ReleaseContext(pContext);

	delete[] pBuffer;

}
Beispiel #2
0
int main(int argc, char * argv[])
{
	std::ifstream fs("C:\\Lookup\\112.127.141.86.html");
	std::string in;
	load_file(in, fs);
	fs.close();
	//static CRegexpT <char> regexp1("\\d+");
	static CRegexpT <char> regexp1("target=\\\"_blank\\\"\\>(.+?)\\</a\\>\\</td\\>");
	// loop
	MatchResult result1 = regexp1.Match(in.c_str());

	while( result1.IsMatched() )
	{  
		//GetEnd匹配成功后,获取所匹配到的子字符串的结束位置。如果匹配失败,则返回负值。
		//GetStart匹配成功后,获取所匹配到的子字符串的开始位置。如果匹配失败,则返回负值。
		printf("%.*s\n", result1.GetEnd() - result1.GetStart(), in.c_str() + result1.GetStart());
		// get next
		result1 = regexp1.Match(in.c_str(), result1.GetEnd());  //返回匹配结果 MatchResult 对象。
		// 通过 MatchResult 对象,可以得知是否匹配成功。如果成功,通过 MatchResult 对象可以获取捕获信息。

	}

	// text
	char * text = "http://www.cppprog.com/2009/0112/48.html";
	// declare
	static CRegexpT <char> regexp("\\d+");
	// loop
	MatchResult result = regexp.Match(text);
	//IsMatched返回非零值表示匹配成功,返回 0 表示匹配失败。
	while( result.IsMatched() )
	{  
		//GetEnd匹配成功后,获取所匹配到的子字符串的结束位置。如果匹配失败,则返回负值。
		//GetStart匹配成功后,获取所匹配到的子字符串的开始位置。如果匹配失败,则返回负值。
		printf("%.*s\n", result.GetEnd() - result.GetStart(), text + result.GetStart());
		// get next
		result = regexp.Match(text, result.GetEnd());  //返回匹配结果 MatchResult 对象。
		// 通过 MatchResult 对象,可以得知是否匹配成功。如果成功,通过 MatchResult 对象可以获取捕获信息。

	}
	return 0;
}
Beispiel #3
0
std::string::size_type Foam::regExp::find(const std::string& str) const
{
    std::string::size_type pos = std::string::npos;

    if (preg_ && !str.empty())
    {
        const MatchResult result = preg_->Match(str.c_str());

        if (0 < result.IsMatched())
        {
            pos = result.GetStart();
        }
    }

    return pos;
}