最近在做的东西要用到正则解析HTML文本,据说开源的boost库很不错,于是到http://sourceforge.net/project/showfiles.php?group_id=7586下载最新版本的源代码 boost_1_34_1.zip,20多M,解压到本地硬盘竟然200多M...
总结一下vs2005安装boost正则流程
1:解压到C:\boost\boost_1_34_1\完成后,打开Visual Studio 2005 命令提示
2:cd C:\boost\boost_1_34_1\libs\regex\build
3:nmake vc8.mak
提示错误: "nmake"不是内部命令或外部命令,也不是可运行程序
搜索一下,原来是环境变量未设置
在 我的电脑 > 属性 > 高级 > 环境变量 我加了两个变量值 C:\Program Files\Microsoft Visual Studio 8\VC\include,C:\Program Files\Microsoft Visual Studio 8\VC\lib
终于可以运行了
4:打开vs2005新建一个测试程序
下面这个在网上找的
#include "stdafx.h"
#include <cstdlib>
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace boost;
regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
int main(int argc, char* argv[])
{
std::string in;
cmatch what;
cout << "enter test string" << endl;
getline(cin,in);
if(regex_match(in.c_str(), what, expression))
{
for(int i=0;i<what.size();i++)
cout<<"str :"<<what[i].str()<<endl;
}
else
{
cout<<"Error Input"<<endl;
}
return 0;
}
提示找不到 boost/regex.hpp
fatal error C1083: 无法打开包括文件:“boost/regex.hpp”: No such file or directory
解决办法:在vs工具>选项>项目和解决方案>vc++目录 ---显示以下内容的文件和目录 在库文件选项里,添加路径 C:\boost\boost_1_34_1\libs\regex\build\vc80
包含文件选项里,添加路径 C:\boost\boost_1_34_1
到现在终于可以编译运行了...
但是在生成release项目的时候又提示:无法打开文件“libboost_regex-vc80-mt-gd-1_34_1.lib”,根本就没有这个文件,怎么会连接到这个文件呢?真令人想不通,不管了,先把libboost_regex-vc80-mt-gd-1_34.lib复制一份改下名称,终于没问题了
去除HTML标签
#include <string >
#include <boost/regex.hpp >
boost::regex reg("<style(.+?)/style>|<script(.+?)/script>|<[^>]+>|[\n]*",boost::regex::icase |boost::regex::perl);
std::string s=boost::regex_replace(std::string((char *)pbyBuffer),reg,"");
pbyBuffer是 BYTE *,下载的html代码地址
s就是去除html标签后的文本
<style(.+?)/style> 过滤 style
<script(.+?)/script> 过滤script
<[^>]+> 过滤 < > 中间的所有代码
[\n]* 换行符