Unescape tab and newline escapes

28 Apr 2010

You want to use regular expressions in perl to turn the literal '\t' and '\n' two character sequences into the actual tab and newline characters.

The trivial solution doesn’t work if you also want to allow the use of '\\' to escape the “escape” sequence (e.g. so that '\\node' expands to '\node', but '\\\n' does expand to a blackslash followed by a newline).

What you actually need, is something like


Small discussion: this first substitution encapsulates every escape sequence '\X' into '\X]', so that the escaped 'X' can not interfere with the rest of the string. Then you can evaluate all the escapes you want, e.g. turn '\n]' into a newline. Finally all remaining '\X]' are expanded back to 'X'.