in reply to Regexes and backslashes
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regexes and backslashes
by oko1 (Deacon) on Feb 17, 2011 at 00:09 UTC | |
by 7stud (Deacon) on Feb 17, 2011 at 03:56 UTC | |
by Anonymous Monk on Feb 17, 2011 at 04:05 UTC | |
by 7stud (Deacon) on Feb 17, 2011 at 03:56 UTC |