use XML::Twig; my $twig = XML::Twig->new( twig_handlers => { div => sub { my $class = $_->att("class") // ""; $_->delete if $class eq "topsearchbar"; }, } ); $twig->parse_html(\*DATA)->print; __END__ Each of the unwanted divs at this point