Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Regexes and backslashes

by ELISHEVA (Prior)
on Feb 16, 2011 at 10:05 UTC ( [id://888478]=note: print w/replies, xml ) Need Help??


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

    > 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'?

    I don't want '\n' remapped at all. The only three things that I do want remapped are \, \; \\n - everything else should just be left as is.

    > A hash isn't necessary if you use y///.

    But I'm not trying to replace anything like a list of similar metacharacters. The whole challenge here is that I'm dealing with a set of things that are unlike each other: two sets of literal characters and one that is a literal plus a metacharacter. I'm just wondering if they can be handled by a common mechanism; that's what I'm asking about.

    Thank you for trying.

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats
      To get a two character literal '\n' to convert to a newline "\n", which is not two characters,I think you have to use eval().
        No, its already been demonstrated that you don't have to use eval.
      To get a two character literal '\n' to convert to a newline "\n", which is not two characters, I think you have to use eval().

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://888478]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found