regular expression using ASCII symbols in PERL replace string

Posted on 2006-05-20
Last Modified: 2012-06-27
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
Question by:dhowes_UK
    LVL 4

    Expert Comment

    these work:

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

    so you could do things like:
    s/\x40/ AT /;
    s/\x{3078}/ some chinese thing /;
    LVL 6

    Expert Comment

    Yeah, you can use the escape characters. Just put then in quotes

    print "\x41"; # prints the character A

    LVL 84

    Accepted Solution

    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.
    LVL 84

    Expert Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now