sub munge { my $str = shift; $str =~ s{^R(?:([12])|(3))?$} {(defined $1 ? $1 : '') . 'text' . (defined $2 ? $2 : '')}e; $str; }