my $str = 't?e\\\\xt\\\\* with escapes\\*'; my %swap; @swap{'?','*'} = ('#','?'); $str=~s/((?:\\)(.)|\?|\*)/$swap{$1}||$2/ge; print ">>$str<<\n";