in reply to Replace only unescaped metachars

So, applying the idea I linked to above, we get this.

$_ = "t?e\\\\xt\\\\* with escapes\\*\n"; s/\\\\/\\s/g, s/(?<!\\)\?/#/g, s/(?<!\\)\*/?/g, s/\\(\W)/$1/g, s/\\s/\ +\/g; print;
This outputs
t#e\xt\? with escapes*

Update: without lookarounds (this is what you'd do if all you had was sed):

$_ = "t?e\\\\xt\\\\* with escapes\\*\n"; s/\\\\/\\s/g, s/\\\?/\\S/g, s/\\\*/\\T/g, s/\?/#/g, s/\*/?/g, s/\\(\W) +/$1/g, s/\\S/?/g, s/\\T/\*/g, s/\\s/\\/g; print;

Replies are listed 'Best First'.
Re^2: Replace only unescaped metachars
by Anonymous Monk on Feb 23, 2007 at 11:01 UTC
    Oh, but I would consider this to be cheating ;-)
    I mean the temporary replacements. Your solution reduces the complexity instead of mastering it.
    Of course I agree that this often is a good strategy but I was curious if it is possible to handle the three tasks (unescape, conversion, different treatment of escaped and unescaped metachars) at the same time, only allowing a little pre- and/or postprocessing perhaps.

    But still, thanks for joining in!
    -Michael