use warnings; use strict; use HTML::TreeBuilder::XPath; my $str = q{ not an img and text }; my $root = HTML::TreeBuilder->new_from_content($str); foreach my $tag ($root->findnodes( '//a[./img]')) { print "link: ", $tag->as_HTML; }