You may also be interested in HTML::TreeBuilder. Consider:

use warnings; use strict; use HTML::TreeBuilder; my $html = <<'HTML'; <a href="URL1"> text1.0 <img src="SRC1"> text1.1 <br> <garbage2> text1.2 <a href="URL2"> text2.0 <img src="SRC2"> text2.1 <br> <garbage2> <garbage3> text2.2 <a href="URL3"> text3.0 <img src="SRC3"> text3.1 <br> <garbage3> text3.2 <x1 a="a" b="b"> text3.3 </a> text2.3 <haha> </a> text1.3 <oho> <a href="URL4"> text4.0 <img src="SRC4"> text4.1 <br> <garbage4> text4.2 <x1 a="a" b="b"> text4.3 </a> text1.4 </a> HTML my $tree = HTML::TreeBuilder->new_from_content ($html); for my $elt ($tree->look_down ('_tag', 'a')) { print "A " . $elt->attr ('href') . "\n\tTEXT: '"; my @text_segs; for my $child ($elt->content_list ()) { next if ref $child and $child->{_tag} ne 'a'; last if ref $child; push @text_segs, $child; } print "$_ " for @text_segs; print "\n"; }

Prints:

A URL1 TEXT: ' text1.0 text1.1 text1.2 A URL2 TEXT: ' text2.0 text2.1 text2.2 A URL3 TEXT: ' text3.0 text3.1 text3.2 text3.3 A URL4 TEXT: ' text4.0 text4.1 text4.2 text4.3

DWIM is Perl's answer to Gödel

In reply to Re: Parsing HTML - once again by GrandFather
in thread Parsing HTML - once again by Krambambuli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.