2010-09-03 132 views
-1

我想匹配出这个文本:多行正则表达式

<a href="http://english317.ning.com/profiles/blogs/bad-business-writing-487">Continue</a> 
             </div> 
       <p class="small"> 

                Added by <a href="/profile/KemberleyRamirez">Kemberley Ramirez</a> on September 2, 2010 at 11:38pm 

我想获得后的文字/博客(如“坏企业写作-487”)和还添加了字符串(学生姓名和提交日期)(例如“Kemberley拉米雷斯在2010年9月2日下午11时38”)

我使用UltraEdit与Perl表达式。

+0

您可能会发现这个网站有用:??regexlib.com/ – vlood 2010-09-03 08:17:19

+5

[朋友不会让朋友们解析HTML正则表达式。(HTTP:/ /stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Ether 2010-09-03 14:44:53

+0

我没有问我是否应该;我问了如何。在这种情况下完全可行,因为这些标签通常在同一个地方用REGEX解析。 – Caveatrob 2010-09-04 07:54:21

回答

3

我不知道你想搭配什么,但你最好使用适当的HTML解析器:

#!/usr/bin/perl 

use strict; use warnings; 

use HTML::TokeParser::Simple; 

my $parser = HTML::TokeParser::Simple->new(\*DATA); 

my $blog_re = qr{^http://english317.ning.com/profiles/blogs/(.+)\z}; 
my $profile_re = qr{^/profile/(\w+)\z}; 

while (my $tag = $parser->get_tag('a')) { 
    next unless my ($href) = $tag->get_attr('href'); 
    if ($href =~ $blog_re or $href =~ $profile_re) { 
     print "[$1]\n"; 
    } 
} 

__DATA__ 
<a href="http://english317.ning.com/profiles/blogs/bad-business-writing-487">Continue</a> 
             </div> 
       <p class="small"> 

                Added by <a href="/profile/KemberleyRamirez">Kemberley Ramirez</a> on September 2, 2010 at 11:38pm 
-1

/s和/ m修饰符控制如何处理多行。 看到perlretut

你可能要像带/ s修饰词,像这样RRR reg.exps:(未经测试)

$foo =~ m|blogs/([^"]+).*Added by <[^>]+>([^<]+)</a>|s 

以间||而不是//避免一切逃逸..

-2

以下应多行工作:

.*blogs\/(\S+)".*\(\n.*\)*<a.*>(.*)<\/a>(.*) 
0

在 “点相匹配换行” 模式下使用PowerGrep,我想出了这个:

(?>profiles/blogs/(.*?)").*?added by(.*?)</a>(.*?2010.*?\d{2}[ap]m) 

(然后一个额外的处理搜索) <一个*>