my $str = q{ bla bla a bunch of html and text that will change from file to file }; use HTML::TreeBuilder; my $t = HTML::TreeBuilder->new_from_content($str); foreach ($t->look_down('_tag', 'span')) { next if $_->attr('id') ne 'editArea' || $_->attr('name') ne 'whatever'; $_->delete_content; $_->push_content('this is some replacement text'); } print $t->as_HTML; $t->delete;