use warnings; use strict; use XML::Twig; my $x = ' Some text CHANGE_THIS dont change this CHANGE_THIS dont change this DONT CHANGE THIS dont change this CHANGE_THIS dont change this DONT CHANGE THIS '; my $t = XML::Twig->new( twig_handlers => { rstate => \&rstate }, pretty_print => 'indented', ); $t->parse($x); $t->print(); sub rstate { my ($t, $rstate) = @_; my $text = $rstate->text(); $text =~ s/CHANGE_THIS/CHANGED/; $rstate->set_text($text); } __END__ Some text CHANGED dont change this CHANGED dont change this DONT CHANGE THIS dont change this CHANGED dont change this DONT CHANGE THIS