2.3 常用函数
boost::regex_match:匹配函数,用来判断所给的字符串与是否匹配,返回结果为bool型。
boost::regex_search:查找函数,用来在给定的字符串中寻找与给定正则表达式匹配的特定模式。
boost::regex_replace:替换函数,用来在给定的字符串中搜索所有给定的模式,对于每个匹配的部分,通过调用match_result::format来格式化字段,并将结果输出。
3 网页信息抽取
3.1网页信息抽取类CHtmlcodeIE设计与实现
(1)CHtmlcodeIE成员函数定义如下:
class CHtmlcodeIE
{
public:
CHtmlcodeIE();
virtual ~CHtmlcodeIE();
public:
CString DeleteScriptTag(CString htmlcode); //去除脚本样式注释
CString ExtractAllText(CString htmlcode); //抽取所有文本
CString ExtractText(CString htmlcode); //抽取所有非空文本
CString ExtractTextAImg(CString htmlcode); //抽取文本链接图片
CString ExtractTextA(CString htmlcode); //抽取文本链接对
CString ExtractImg(CString htmlcode); //抽取所有图片
CString DeleteTag(CString HtmlCode); //过滤文本中的无用代码
};
(2)CHtmlcodeIE成员函数实现如下:
#include "iostream.h"
#include "string.h"
#include <boost/regex.hpp> //regex头文件
using namespace std; //标准名字空间
#define Para boost::regbase::normal|boost::regbase::icase
CString CHtmlcodeIE::DeleteScriptTag(CString htmlcode)
{
std::string s=(string)htmlcode; // CString类型转换位stirng类型 boost::regex expression("(<script.*?>)(.*?)(</script>)", Para);//去除脚本
|