If '\\n' (3 chars) is supposed to map to a single newline, what did you want '\n' to map to? '\n' (2 chars) or 'n'?
A hash isn't necessary if you use y///. I also wouldn't worry about the e modifier in this case. You are doing nothing more in the second half of substitution than what you would have to do anyway with an arbitrary string to untaint it: testing if it is defined and eliminating and substituting characters. These examples work with all one character backslash sequences (\t\n\r\f\b\a\e), not just newlines.
For '\n' to '\n' (2 chars):
s /\\([^\\tnrfbae])|\\\\([tnrfbae]) /(my$x=defined($1)?$1:$2)=~y#tnrfbae#\t\n\r\f\b\a\e#;$x; /gex;
For '\n' to 'n', this regex would do it:
s /\\([^\\tnrfbae])|\\\\([tnrfbae])|\\([tnrfbae]) /(my$x=defined($1)?$1:$2?$2:'')=~y#tnrfbae#\t\n\r\f\b\a\e#;$x?$x:$ +3; /gex;
In reply to Re: Regexes and backslashes
by ELISHEVA
in thread Regexes and backslashes
by oko1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |