• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

regular expression using ASCII symbols in PERL replace string

Forgive this question from a newbie, but is it possible to use non-printing ASCII chars in a PERL replace string, with an escape sequence like you can in the search string e.g. \x0D \x0A etc

If not how would one achieve this? \r \n etc is not sufficient here.

Also is it possible to include Unicode chars as escape sequences in the replace string?

Thanks all
  • 2
1 Solution
these work:

Unicode like: \x{3078}
ASCII like: \x40

so you could do things like:
s/\x40/ AT /;
s/\x{3078}/ some chinese thing /;
Yeah, you can use the escape characters. Just put then in quotes

print "\x41"; # prints the character A

Yes, it is possible to use the same escape characters in the replace string like you can in the search string, or any interpolating string.
perldoc perlop
       Quote and Quote-like Operators

       While we usually think of quotes as literal values, in Perl they func-
       tion as operators, providing various kinds of interpolating and pattern
       matching capabilities.  Perl provides customary quote characters for
       these behaviors, but also provides a way for you to choose your quote
       character for any of them.  In the following table, a "{}" represents
       any pair of delimiters you choose.

           Customary  Generic        Meaning        Interpolates
               ''       q{}          Literal             no
               ""      qq{}          Literal             yes
               ``      qx{}          Command             yes*
                       qw{}         Word list            no
               //       m{}       Pattern match          yes*
                       qr{}          Pattern             yes*
                        s{}{}      Substitution          yes*
                       tr{}{}    Transliteration         no (but see below)
               <<EOF                 here-doc            yes*

               * unless the delimiter is ''.
       The following escape sequences are available in constructs that inter-
       polate and in transliterations.

           \t          tab             (HT, TAB)
           \n          newline         (NL)
           \r          return          (CR)
           \f          form feed       (FF)
           \b          backspace       (BS)
           \a          alarm (bell)    (BEL)
           \e          escape          (ESC)
           \033        octal char      (ESC)
           \x1b        hex char        (ESC)
           \x{263a}    wide hex char   (SMILEY)
           \c[         control char    (ESC)
           \N{name}    named Unicode character

       The following escape sequences are available in constructs that inter-
       polate but not in transliterations.

           \l          lowercase next char
           \u          uppercase next char
           \L          lowercase till \E
           \U          uppercase till \E
           \E          end case modification
           \Q          quote non-word characters till \E


Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now