use XML::LibXML; my $doc = XML::LibXML->load_html( location => "example.html", { recover => 1 } ); my @ids2text = map { [ $_->value, $_->getOwnerElement->textContent ] } $doc->findnodes('//@id'); $_->[1] =~ s/\W+//g for @ids2text; print join ", ", map sprintf("%s=%s", @$_), @ids2text;