my $string = 't?e\\\\xt\\\\* with escapes\\*'; my $result = ''; while (not $string =~ m/\G\z/gc) { if ($string =~ /\G([^\\*?]+)/gc) { # normal chars $result .= $1; } elsif ($string =~ /\G\\(.)/gc) { # escaped $result .= $1; } elsif ($string =~ /\G\*/gc) { # unescaped * $result .= '?'; } elsif ($string =~ /\G\?/gc) { # unescaped ? $result .= '#'; } } $string = $result; print $string; # prints 't#e\xt\? with escapes*'