in reply to Replace only unescaped metachars

A little different way does it with one s///, but some other helpers.
my $str = 't?e\\\\xt\\\\* with escapes\\*'; my %swap; @swap{'?','*'} = ('#','?'); $str=~s/((?:\\)(.)|\?|\*)/$swap{$1}||$2/ge; print ">>$str<<\n";
Ikegami points out that the (?:\\) can just be \\.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Replace only unescaped metachars
by Anonymous Monk on Feb 23, 2007 at 08:02 UTC
    Oh, this one is clever _and_ readable. The /e might not be the most efficient but that is more than outweight by the clearness of the solution -- just one not too long regex!

    Thank you all for your help, I really learnt a lot with this little problem!

    Michael
      You might be thinking of eval EXPR (/ee) when you mentioned efficiency. /e doesn't cause any code to be compiled at run-time.