Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex HTML内容正则表达式-perl_Regex_Perl - Fatal编程技术网

Regex HTML内容正则表达式-perl

Regex HTML内容正则表达式-perl,regex,perl,Regex,Perl,我的html内容如下所示: html code ... </div>content1</div> html code ... html code ... </div>content2</div> html code ... html代码。。。content1 html代码。。。 html代码。。。content2 html代码。。。 我想提取内容1/2/3。。。从HTML作为内容1新行内容2新行内容3有什么想法吗?提前谢谢 以下是一个使用的示

我的html内容如下所示:

html code ... </div>content1</div> html code ... 
html code ... </div>content2</div> html code ...
html代码。。。content1 html代码。。。
html代码。。。content2 html代码。。。
我想提取内容1/2/3。。。从HTML作为内容1新行内容2新行内容3有什么想法吗?提前谢谢

以下是一个使用的示例,灵感来自:


如果你研究过你的问题,你会发现几十篇,可能几百篇,告诉你不要用正则表达式来解析HTML的帖子。有几个非常好的Perl模块可以为您实现这一点,而且一个正则表达式解决方案很可能迟早会崩溃,这要感谢大家的努力
#!/usr/bin/env perl

use strict ;
use warnings ;

use Mojo::DOM ;

my $html = <<EOHTML;
<!DOCTYPE html>
<html>
<head>
<title>Sample HTML with 2 divs</title>
</head>
<body>
     <div>
        Four score and seven years ago our fathers brought forth on this
        continent a new nation, conceived in liberty, and dedicated to the
        proposition that all men are created equal.
     </div>
     <div>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
     </div>
</body>
</html>
EOHTML

my $dom = Mojo::DOM->new ;

$dom->parse( $html ) ;

for my $div ( $dom->find( 'div' )->each ) {

    print $div->all_text . "\n" ;

}
Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.