in reply to Trying to parse html file

<p..><some more tags> --- TITLE --- WORDS WORDS WORDS WORDS WORDS WORD +S<BR> <object...><a lot more tags></object> </p>
WORDS section runs onto multiple lines. The code within the object tags run multiple lines as well. I'd like to grab the title, words, and object code of each instance on a page. The html is being stored in a scalar.

That's enough info for working out a basic solution with HTML::Parser. (There's still the matter of what you need to do with these pieces once you have them, but that's the easy part, right?)

It really helps to have valid html data as input for trying this out, so I made a few changes to the example you gave. Okay, this code is a lot longer than a couple regexes, but it will work, reliably, for any valid html data that resembles your example.

use strict; use HTML::Parser; my $html = <<EOH; <p foo="bar"><some more tags> --- TITLE --- WORDS WORDS WORDS WORDS WORDS WORDS<BR> <object att="val"> <and> <more> <tags/> </more> </and></object> </p> EOH my ( $partext, $objtext ); my ( @titlewords, @objects ); my $inpar = my $inobj = 0; my $parser = new HTML::Parser( api_version => 3, start_h => [ \&handle_starttag, "tagnam +e,text" ], text_h => [ \&handle_text, "dtext" ], end_h => [ \&handle_endtag, "tagname,te +xt" ] ); $parser->parse( $html ); for my $t ( @titlewords ) { print "=== Found title and words: ===\n$t\n======\n"; } for my $o ( @objects ) { print "=== Found object: ===\n$o\n======\n"; } sub handle_starttag { my ( $tag, $text ) = @_; if ( $tag eq 'p' ) { $inpar = 1; $partext = ''; } elsif ( $tag eq 'object' ) { $inobj = 1; $objtext = ''; } elsif ( $tag eq 'br' and $inpar ) { push @titlewords, $partext if ( $partext =~ /-+ TITLE -+/ ); $inpar = 0; } elsif ( $inobj ) { $objtext .= $text; } } sub handle_text { my ( $text ) = @_; if ( $inpar ) { $partext .= $text; } elsif ( $inobj ) { $objtext .= $text; } } sub handle_endtag { my ( $tag, $text ) = @_; if ( $tag eq 'object' ) { push @objects, $objtext; $inobj = 0; } elsif ( $inobj ) { $objtext .= $text; } }

Look at the man page for HTML::Parser to understand how the "new" call works. The rest is just a matter of figuring out how to handle the data contents, as the parser encounters the relevant tag and text events.

(Updated the code to check for "TITLE" in the text when there's a "br" tag, and added line-breaks in the html to show how those would be handled.)