$ perl -Mstrict -Mwarnings -E ' my $string = "---------xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy--------"; $string =~ s/^-*(.*?)-*$/?$1?/; say $string; ' ?xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy?