in reply to efficient char escape sequence substitution

This is safer than an overall string-eval, and seems to work as desired, replacing only backslash-escapes:
use strict; use warnings; s/(\\(?:\W|\w{1,3}))/qq("$1")/gee, print for <DATA>; __DATA__ this is a string\t$foowith some text\r\n this is a string\010with some\x09text\r\n

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: efficient char escape sequence substitution
by nobull (Friar) on May 28, 2005 at 07:51 UTC
    That doesn't handle wide-character escapes.

    In comp.lang.perl.misc I recently offered this solution.

    s/(\\[^"\$\@]+)/qq("$1")/eeg;

    Note that's not 100% infalible but AFAIK it's not a security problem and copes with all reasonable strings. It does have the interesting side effect of stripping \$ \" and \@ - but those have no buisness being in most of the sort of strings you'll encounter.

      How wide is a wide character escape? There's no reason that you can't change the quantifier in my regex to accommodate them. I just wasn't aware of any escapes wider than three characters.

      Caution: Contents may have been coded under pressure.
        I don't think there's a fixed maximum \N{WHITE SMILING FACE}.
Re^2: efficient char escape sequence substitution
by mifflin (Curate) on May 27, 2005 at 23:08 UTC
    Can you help me understand your regex modifiers?
    gee
    Where in perldoc can I find an explanation?
      Documentation.

      g means global replace. e means eval the substitution space before substituting it. Repeating the e means eval it again. So it starts out looking like the string qr("\\t"); after an eval, it looks like "\t", and after a 2nd eval, it's just a tab. (Actually, it may have captured more than just the tab, but anything after the tab is left intact.)


      Caution: Contents may have been coded under pressure.