C++ 提取网页内容系列之四正则

标 题: C++ 提取网页内容系列之四
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/4173833.html 

欢迎转帖 请保持文本完整并注明出处


将网页内容下载后存入字符串string 或者本地文件后 我们开始进行搜索和查询 获取信息
这里使用正则式  使用vs2008  其自带的tr1库(预备标准库) 有正则式库供使用
带头文件/*******************************************************************************
*  @file        
*  @author      def< qq group: 324164944 >
*  @blog        http://www.cnblogs.com/itdef/
*  @brief     
/*******************************************************************************/
#include <regex>
using namespace std::tr1;
using namespace std;

这里推荐正则式教程
正则表达式30分钟入门教程
http://www.cnblogs.com/deerchao/ ... zhongjiaocheng.html

C++:Regex正则表达式
http://blog.sina.com.cn/s/blog_ac9fdc0b0101oow9.html

 

首先来个简单例子

#include <string>
#include <iostream>
#include <regex>

using namespace std::tr1;
using namespace std;

string strContent = " οnclick=\"VeryCD.TrackEvent('base', '首页大推', '神雕侠侣');";

void Test1()
{
        string strText = strContent;
        string strRegex = "首页大推";
        regex regExpress(strRegex);

        smatch ms;

        cout << "*****************************" << endl;
        cout << "Test 1" << endl << endl;

        while(regex_search(strText, ms, regExpress))
        {
                for(string::size_type i = 0;i < ms.size();++i)
                {
                        cout << ms.str(i).c_str() << endl;
                }
                strText = ms.suffix().str();
        }

        cout << "*****************************" << endl << endl;
}

void Test2()
{
        string strText = strContent;
        string strRegex = "首页大推.*'(.*)'";
        regex regExpress(strRegex);

        smatch ms;

        cout << "*****************************" << endl;
        cout << "Test 2" << endl << endl;
        while(regex_search(strText, ms, regExpress))
        {
                for(string::size_type i = 0;i < ms.size();++i)
                {
                        cout << ms.str(i).c_str() << endl;
                }
                strText = ms.suffix().str();
        }
        cout << "*****************************" << endl << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
        Test1();
        Test2();
                
        return 0;
}

Test1中 我们等于是直接搜索字符串 然后 打印出找到的位置Test2中 我们使用 首页大推.*'(.*)'  
.号等于是任意非空白换行字符 *则代表重复任意多次(0-无穷次)
而括号表示一个字符集 也就是我们需要查找的内容 
请注意这个括号是在 ' '  之间的  也就是查找 首页大推 任意字符之后 两个 '  '号之间的内容

效果如下:
而且我们也发现 ms的显示规律 他首先显示符合条件的字符串 然后现实符合( )里面条件的子字符串

 

下面来个深入点得  分析这个字符串
string strContent0 = "alt=\"火影忍者\" /><div class=\"play_ico_middle\"></div><div class=\"cv-title\" style=\"width:85px;\">更新至612集</div>";

我们使用的正则式规则为 string strRegex = "alt=\"([^\"]*)\".*width:85px;\">(.*)</div>";
注意里面有两个括号  一个是在alt= 之后 在两个" " 之间的内容  一个是在width:85px;\">  和 </div> 之间的内容 
注意  "的显示 由于C++语言的特性 必须写成 \"
现在分析两个括号内容 ([^\"]*)     (.*)

(.*)无须多说  就是任意非空白字符 而且是在width:85px;\">  和 </div> 之间的内容 
([^\"]*)  就是说 非"的内容任意重复多次  而且这个括号是在alt= 之后 在两个" " 之间的内容

运行结果如下:(为了不显示过多内容 符合条件的内容没有全部显示 只显示了符合括号需求的子字符串)

 

/*******************************************************************************
*  @file        
*  @author      def< qq group: 324164944 >
*  @blog        http://www.cnblogs.com/itdef/
*  @brief     
/*******************************************************************************/


#include <string>
#include <iostream>
#include <regex>

using namespace std::tr1;
using namespace std;



string strContent = " οnclick=\"VeryCD.TrackEvent('base', '首页大推', '神雕侠侣');";

string strContent0 = "alt=\"火影忍者\" /><div class=\"play_ico_middle\"></div><div class=\"cv-title\" style=\"width:85px;\">更新至612集</div>";

void Test1()
{
        string strText = strContent;
        string strRegex = "首页大推";
        regex regExpress(strRegex);

        smatch ms;

        cout << "*****************************" << endl;
        cout << "Test 1" << endl << endl;

        while(regex_search(strText, ms, regExpress))
        {
                for(string::size_type i = 0;i < ms.size();++i)
                {
                        cout << ms.str(i).c_str() << endl;
                }
                strText = ms.suffix().str();
        }

        cout << "*****************************" << endl << endl;
}

void Test2()
{
        string strText = strContent;
        string strRegex = "首页大推.*'(.*)'";
        regex regExpress(strRegex);

        smatch ms;

        cout << "*****************************" << endl;
        cout << "Test 2" << endl << endl;
        while(regex_search(strText, ms, regExpress))
        {
                for(string::size_type i = 0;i < ms.size();++i)
                {
                        cout << ms.str(i).c_str() << endl;
                }
                strText = ms.suffix().str();
        }
        cout << "*****************************" << endl << endl;
}

void Test3()
{
        string strText = strContent0;
        string strRegex = "alt=\"([^\"]*)\".*width:85px;\">(.*)</div>";
        regex regExpress(strRegex);

        smatch ms;

        cout << "*****************************" << endl;
        cout << "Test 3" << endl << endl;

        while(regex_search(strText, ms, regExpress))
        {
                for(string::size_type i = 0;i < ms.size();++i)
                {
                        if(i > 0)
                                cout << ms.str(i).c_str() << endl;
                }
                strText = ms.suffix().str();
        }
        cout << "*****************************" << endl << endl;

}


int _tmain(int argc, _TCHAR* argv[])
{
        Test1();
        Test2();
        Test3();
                
        return 0;
}

 

转载于:https://www.cnblogs.com/itdef/p/4173833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值