Possible Duplicate:
How can I manually interpolate string escapes in a Perl string?
I’m reading a string from a particular file. The problem with it is that it contains escaped characters, like:
Hello!\nI\'d like to tell you a little \"secret\"...
I’d like it to be printed out without escape sequences, like:
Hello!
I'd like to tell you a little "secret".
I thought about removing single backslashes and replacing double with single (since \ is represented as \\), but that doesn’t help me with the \n, \t issues and so on. Before trying to fiddle with ugly, complex replace strings I thought I’d ask – maybe Perl has a built-in mechanism for such transformation?
For Perl single character backslash escapes, you can do this safely using a two character
evalas part of the substitution. You need to put in the characters that are acceptable to interpret in the character class after the\, and then the single character after iseval‘d and inserted into the string.Consider:
Output:
The line
s/\\([rnt'"\\])/"qq|\\$1|"/geedoes the work.The
\\([rnt'"\\])has the acceptable characters to eval inside the braces.The
geepart does a double eval on the replacement string.The
"qq|\\$1|"part is eval’d twice. The firstevalreplaces$1into the string, and the second performs the interpolation.I cannot think of a two character combination here that would be a security breach…
This method does not deal with the following properly:
Quoted strings. For example, Perl would not unescape the string ‘line 1\nline 2’ because of the single quotes.
Escapes sequences that are longer than a single character, such as hex
\x1bor Unicode such as\N{U+...}or control sequences such as\cDAnchored escapes, such as \LMAKE LOWER CASE\E or \Umake upper case\E
If you want more complete escape replacement, you can use this regex:
That handles all Perl escapes except:
Anchored type (\Q, \U, \L ended by \E)
Quoted forms, such as
'don't \n escape in single quotes'or[not \n in here]named unicode characters, such as
\N{THAI CHARACTER SO SO}Control characters like
\cD(that is easily added…)But that was not part of your question as I understood it…