in reply to Regexes and backslashes

my $text = 'Goodbye\; Good luck\, and thanks for all the fish!\\n\\n'; $text =~ s{ \\ ([^n]) } {$1}xmsg; print "-->$text<--"; --output:-- -->Goodbye; Good luck, and thanks for all the fish!\n\n<--

Replies are listed 'Best First'.
Re^2: Regexes and backslashes
by oko1 (Deacon) on Feb 16, 2011 at 05:01 UTC

    Oh, dear. I guess I'm extra awful at explaining what I mean tonight. Pity; I suppose the only thing left is to shoot myself and get it over with. :)

    Your example does indeed result in the above output - but that's not what I'm looking for. I would like for the escaped semicolons to be converted to non-escaped semicolons; for escaped commas to be converted to non-escaped commas; and for escaped newlines to be converted to actual newlines. Metacharacters, not literal '\n's. Ones that actually result in newlines being produced on the screen when a line is printed - i.e.,

    Line 1:Goodbye; Good luck, and thanks for all the fish!
    Line 2: 

    and NOT

    Line 1:Goodbye; Good luck, and thanks for all the fish!\n\n

    (I'm not trying to come across as being snarky; I'm just trying to make *sure* that I'm getting across what I mean, since I've obviously failed to do so before now.)

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats

      OK, how about this one. It's a little bit klunky in its use of a conversion hash, but the hash can easily be expanded as needed.

      >perl -wMstrict -le "$_ = 'Goodbye\; Good luck\, and thanks for all the fish!\n\n'; my %conv = ( ',' => ',', ';' => ';', n => qq{\n} ); print qq{[[$_]]}; s{ \\([,;n]) }{$conv{$1}}xmsg; print qq{[[$_]]};; " [[Goodbye\; Good luck\, and thanks for all the fish!\n\n]] [[Goodbye; Good luck, and thanks for all the fish! ]]

        Heh. Yeah, we could even in-line it:

        s|\\([,;n])|${{',',',',';',';',n=>"\n"}}{$1}|g;

        Nicely confusing. :)

        I guess there really is no way to make this work with a simple char class replacement, though. Oh well.

        -- 
        Education is not the filling of a pail, but the lighting of a fire.
         -- W. B. Yeats

      Aye, this does make it more clear, although I already understood what you meant in the end.

      The problem of course was your extra backslashes in the original string confused your intent.

      $_ = 'Goodbye\; Good luck\, and thanks for all the fish!\\n\\n';

      Being synonymous with and more simply stated as

      $_ = 'Goodbye\; Good luck\, and thanks for all the fish!\n\n';

      I certainly understand your beef with evaling, but given you know the content of the eval to a certainty, I don't believe there is any security risk. Would be interested if proven wrong there though.