use strict; use warnings; use Test::More tests => 6; is(munge('R'), 'text', 'R'); is(munge('R1'), '1text', 'R1'); is(munge('R2'), '2text', 'R2'); is(munge('R3'), 'text3', 'R3'); # this here is the trickiness is(munge('R13'), 'R13', 'R13 unchanged'); is(munge('R4'), 'R4', 'R4 unchanged'); sub munge { my $str = shift; no warnings; $str =~ s/^R(?:([12])|(3))?$/${1}text$2/; $str; } #### sub munge { my $str = shift; $str =~ s{^R(?:([12])|(3))?$} {(defined $1 ? $1 : '') . 'text' . (defined $2 ? $2 : '')}e; $str; }