my $document = new My::String( read_file('doc.txt') ); my @sections = $document->split( qr/^------\n/ ); my @markers = $sections[2]->match( qr/{{\w+}}/ ); foreach $marker (@markers) { # $marker will be a My::String object my $old = $marker->text(); my $new = ''; # ... # Complex code to generate replacement text, depending on the # exact contents of $old, goes here # ... $marker->setText( $new ); # change propagates to $document } write_file( 'doc.txt', $document->text() );