in reply to Re: reverse quotemeta?
in thread reverse quotemeta?

Your substitution will correctly leave the \n in \\n alone, but what about \\\n? The backslash preceeding \n is itself preceeded by a backslash, so the \n should be converted in that case. \\\\n, on the other hand, shouldn't get the \n converted.

What is needed is something to match pairs of backslashes before the \n, like this: s/((?<!\\)(?:\\\\)*)\\n/$1\n/g;

Replies are listed 'Best First'.
(tye)Re: reverse quotemeta?
by tye (Sage) on Jun 26, 2001 at 22:48 UTC

    If you are going to do that, then you need to unescape the backslashes as well! While you can do that via (something close to one of these):

    s#(?<!\\)((?:\\\\)*)\\n# '\\'x(length($1)/2) . "\n" #ge; # or s#(?<!\\)(\\*)\1\\n#$1\n#g;
    I find that having to escape your backslashes only when they preceed a (desired) newline to be confusing so I'd go with japhy's excellent sugestion or perhaps: s#\\([\\n])# $1 eq "n" ? "\n" : $1 #ge

            - tye (but my friends call me "Tye")