-4

I have html content which look like this:

html code ... </div>content1</div> html code ... 
html code ... </div>content2</div> html code ...

and I would like to extract the content1/2/3... from the HTML as content1 new line content2 new line content3 any idea ? Thanks in advance.

molwiko
  • 323
  • 1
  • 6
  • 14
  • 1
    If you had researched your question at all you would have found dozens, possibly hundreds, of posts telling you *don't use regexes to parse HTML*. There are several very good Perl modules that will do it for you, and a regex solution is very likely to break sooner or later – Borodin Jun 26 '14 at 11:45

1 Answers1

0

Here's an example using Mojo::DOM, inspired by this StackOverflow answer:

#!/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" ;

}

The output is:

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.
Community
  • 1
  • 1
GJoe
  • 185
  • 7