Use HTML::SimpleLinkExtor, HTML::LinkExtor, or one of the other link extracting Perl modules. You don't need a regex at all.
Here's a short example. You don't have to subclass. You just have to tell %HTML::Tagset::linkElements
which attributes to collect:
#!perl
use HTML::LinkExtor;
$HTML::Tagset::linkElements{'a'} = [ qw( href class ) ];
$p = HTML::LinkExtor->new;
$p->parse( do { local $/; <> } );
my @links = grep {
my( $tag, %hash ) = @$_;
no warnings 'uninitialized';
$hash{class} eq 'foo';
} $p->links;
If you need to collect URLs for any other tags, you make similar adjustments.
If you'd rather have a callback routine, that's not so hard either. You can watch the links as the parser runs into them:
use HTML::LinkExtor;
$HTML::Tagset::linkElements{'a'} = [ qw( href class ) ];
my @links;
my $callback = sub {
my( $tag, %hash ) = @_;
no warnings 'uninitialized';
push @links, $hash{href} if $hash{class} eq 'foo';
};
my $p = HTML::LinkExtor->new( $callback );
$p->parse( do { local $/; <DATA> } );