免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2353 | 回复: 5
打印 上一主题 下一主题

求一正则表达式(处理html文件所用) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-25 18:36 |只看该作者 |倒序浏览
需要得到html文件的<h3>...</h3>和<div class="intro"></div>之间的内容,包括<a></a>...
html文件如下:
<div class="generaltitle">
&nbsp;&nbsp;&nbsp;&nbsp;<div class="title">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h3>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;访问Google News的人数超过了CNN
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h3>
&nbsp;&nbsp;&nbsp;&nbsp;</div>
</div>
&nbsp;&nbsp;&nbsp;&nbsp;<div class="details">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="">matrix</a>  发表于 2009年12月24日 18时56分 星期四
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="//it.solidot.org/print.pl?sid=09/12/24/1058201"><img src="//solidot.org/images/print.gif" height="11" width="15" alt="Printer-friendly"></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="//it.solidot.org/email.pl?sid=09/12/24/1058201"><img src="//solidot.org/images/email.gif" height="14" width="20" alt="Email story" ></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>来自<b>QQ弹出窗</b>部门</strong>
&nbsp;&nbsp;&nbsp;&nbsp;</div>
&nbsp;&nbsp;&nbsp;&nbsp;<div class="body">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div class="topic">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="//it.solidot.org/search.pl?tid=87">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="//solidot.org/images/topics/topicmedia.gif" width="60" height="53" alt="媒体" title="媒体" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div class="intro">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Techcrunch<a href="http://www.techcrunch.com/2009/12/23/google-news-cnn/">报道</a>,根据comScore的统计,2009年11月Google News的全球独立访问者数量达到了1亿,超过CNN的6600万和《纽约时报》的9200万,仅次于Yahoo News的1.38亿全球独立访问者。<i>
全球最主要的在线新闻来源分别是:Yahoo News,Google News,《纽约时报》,CNN,中国的腾讯(独立访问者5300万),BBC和MSN News都是4800万,《华尔街日报》在线只有680万。这是否意味着传统新闻网站的衰落呢?《商业周刊》前主编John A. Byrne<a href="http://www.c-changemedia.com/2009/12/google-media-biting-hand-that-feeds-you.html">认为</a>不是,新闻网站不应该屏蔽Google爬虫,这一趋势显示人们更相信Google是一个无偏见的信息源。虽然Google News的全球读者基数超过CNN,但实际上前者更像是一个短暂的中转站,后者则是目的的。11月份,读者在CNN上花的时间超过了20亿分钟,Google News只有8.4亿分钟,CNN的PV数有18亿,而Google News 只有6.56亿。</i>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>






&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</div>
</div>

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
2 [报告]
发表于 2009-12-26 04:50 |只看该作者
sed 's/<[^>]*>//g;s/&nbsp;//g'

论坛徽章:
0
3 [报告]
发表于 2009-12-26 08:46 |只看该作者
print line if line =~ /\<h3\>/ .. line =~ /\<\/h3\>/

print line if line =~ /\<div\ class\=\"intro\"\>/ .. line =~ /\<\/div\>/

论坛徽章:
0
4 [报告]
发表于 2009-12-26 11:42 |只看该作者

回复 #1 yorks 的帖子

perl -ne '$str.=$_;END{print $1,$2 if  $str =~ /<h3>(.*?)<\/h3>.*<div class="intro">(.*?)<\/div>/s}'  urfile

结果

  1.             访问Google News的人数超过了CNN
  2.             
  3.         

  4.             Techcrunch<a href="http://www.techcrunch.com/2009/12/23/google-news-cnn/">报道</a>,根据comScore的统计,2009年11月Google News的全球独立访问者数量达到了1亿,超过CNN的6600万和《纽约时报》的9200万,仅次于Yahoo News的1.38亿全球独立访问者。<i>
  5. 全球最主要的在线新闻来源分别是:Yahoo News,Google News,《纽约时报》,CNN,中国的腾讯(独立访问者5300万),BBC和MSN News都是4800万,《华尔街日报》在线只有680万。这是否意味着传统新闻网站的衰落呢?《商业周刊》前主编John A. Byrne<a href="http://www.c-changemedia.com/2009/12/google-media-biting-hand-that-feeds-you.html">认为</a>不是,新闻网站不应该屏蔽Google爬虫,这一趋势显示人们更相信Google是一个无偏见的信息源。虽然Google News的全球读者基数超过CNN,但实际上前者更像是一个短暂的中转站,后者则是目的的。11月份,读者在CNN上花的时间超过了20亿分钟,Google News只有8.4亿分钟,CNN的PV数有18亿,而Google News 只有6.56亿。</i>
复制代码

[ 本帖最后由 mwm5 于 2009-12-26 11:45 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-12-26 15:16 |只看该作者
HTML还是用DOM处理吧

论坛徽章:
0
6 [报告]
发表于 2009-12-26 15:28 |只看该作者
[xiaopei@bourne ~]$ sed -n -e '/<h3>/, /<\/h3>/p' -e  '/<div class="intro">/, /<\/div>/p' a.html
        <h3>
            访问Google News的人数超过了CNN
            
        </h3>
        <div class="intro">
            Techcrunch<a href="http://www.techcrunch.com/2009/12/23/google-news-cnn/">报道</a>,根据comScore的统计,2009年11月Google News的全球独立访问者数量达到了1亿,超过CNN的6600万和《纽约时报》的9200万,仅次于Yahoo News的1.38亿全球独立访问者。<i>
全球最主要的在线新闻来源分别是:Yahoo News,Google News,《纽约时报》,CNN,中国的腾讯(独立访问者5300万),BBC和MSN News都是4800万,《华尔街日报》在线只有680万。这是否意味着传统新闻网站的衰落呢?《商业周刊》前主编John A. Byrne<a href="http://www.c-changemedia.com/2009/12/google-media-biting-hand-that-feeds-you.html">认为</a>不是,新闻网站不应该屏蔽Google爬虫,这一趋势显示人们更相信Google是一个无偏见的信息源。虽然Google News的全球读者基数超过CNN,但实际上前者更像是一个短暂的中转站,后者则是目的的。11月份,读者在CNN上花的时间超过了20亿分钟,Google News只有8.4亿分钟,CNN的PV数有18亿,而Google News 只有6.56亿。</i>
        </div>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP