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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.