How do I unescape some backslashes but not all in Perl?
I am using tinyperl with the following script:
@lines=<STDIN>;
foreach (@lines) {
s/\\(.)/($1 eq 开发者_如何学Go'"' or $1 eq '\\') ? $1 : '\\' . $1/eg;
print;
}
I would like each backslash to be considered only with the following character, and remove the backslash only if the following character is a double quote or another backslash. (I know this purpose might be unsound to you, but never mind).
For example, I would like to translate abc\ndef\\ghi\"\\\n
to abc\ndef\ghi"\\n
. But this script seems to translate it to abcndef\ghi"\n
instead.
Could you help?
Try
s/\\([\\"])/$1/g;
The []
gives a character class that matches either a backslash or a double quote so we are saying replace a backslash followed by either another backslash or double quote with whichever character in the character class matched.
Looks like you want a look-ahead assertion (see the section in perlre on Extended Patterns).
s/\\(?=[\\"])//g;
精彩评论