my $string = '"foo"'; $string =~ s/"(.*?)"?/_$1_/; # note that you don't have to escape " print $string; # prints __foo"