I agree with Fletch above. This uses HTML::TokeParser::Simple. It pushes all the anchor hrefs and anchor text onto an array. You can then easily choose which you need.

imo this is much easier and more reliable than trying to build, debug and maintain a complex regex on something as loose as HTML.

It's also easy to adapt if the spec changes (and doesn't it always) or to other parsing tasks as they arise.

I believe using a parser (and there are many to suit all tastes) is a big win on all counts and I highly recommend it.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::TokeParser::Simple; my $html; { local $/; $html = <DATA> } my $p = HTML::TokeParser::Simple->new(\$html); $p->unbroken_text(1); my ($in_li, @record, @db); while (my $t = $p->get_token){ $in_li++, next if $t->is_start_tag('li'); next unless $in_li; if ($t->is_end_tag('li')){ push @db, [@record]; $in_li = 0; next; } if ($t->is_start_tag('a')){ push @record, $t->get_attr('href'); my $text = $p->get_trimmed_text('/a'); push @record, $text; } } #die Dumper \@db; # the text inside the first link's text, the 2nd link's URL, the 2nd l +ink's text. for my $record (@db){ my @field = @{$record}; print $field[1], "::", $field[2], "::", $field[3], "\n"; } __DATA__ <li> <a class="style5" href="http://www.site.com/page.html"> some words here </a> - <a class="style3" href="http://www.site.com/page2.html"> "some words here" </a> </li>
output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl some words here::http://www.site.com/page2.html::"some words here" > Terminated with exit code 0.

In reply to Re: 3 capture multi line regex by wfsp
in thread 3 capture multi line regex by Anonymous Monk

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.