for my $context ($doc->findnodes('//span')) { print $context; for my $text ($context->findnodes('text()')) { print $text; while ($text =~ /$re/g) { print $1; } } }