Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:41:20+00:00 2026-05-17T15:41:20+00:00

Possible Duplicate: How can I manually interpolate string escapes in a Perl string? I’m

  • 0

Possible Duplicate:
How can I manually interpolate string escapes in a Perl string?

I’m reading a string from a particular file. The problem with it is that it contains escaped characters, like:

Hello!\nI\'d like to tell you a little \"secret\"...

I’d like it to be printed out without escape sequences, like:

Hello!
I'd like to tell you a little "secret".

I thought about removing single backslashes and replacing double with single (since \ is represented as \\), but that doesn’t help me with the \n, \t issues and so on. Before trying to fiddle with ugly, complex replace strings I thought I’d ask – maybe Perl has a built-in mechanism for such transformation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:41:20+00:00Added an answer on May 17, 2026 at 3:41 pm

    For Perl single character backslash escapes, you can do this safely using a two character eval as part of the substitution. You need to put in the characters that are acceptable to interpret in the character class after the \, and then the single character after is eval‘d and inserted into the string.

    Consider:

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    print "\n\n\n\n";
    
    while (my $data = <DATA>) {
        $data=~s/\\([rnt'"\\])/"qq|\\$1|"/gee;
        print $data;
    }
    
    __DATA__
    Hello!\nI\'d like to tell you a little \"secret\".
    A backslask:\\
    Tab'\t'stop
    line 1\rline 2  (on Unix, "line 1" will get overwritten)
    line 3\\nline 4 (should result in "line 3\\nline 4")
    line 5\r\nline 6
    

    Output:

    Hello!
    I'd like to tell you a little "secret".
    A backslask:\
    Tab'    'stop
    line 2  (on Unix, "line 1" will get overwritten)
    line 3\nline 4 (should result in "line 3\nline 4")
    line 5
    line 6
    

    The line s/\\([rnt'"\\])/"qq|\\$1|"/gee does the work.

    • The \\([rnt'"\\]) has the acceptable characters to eval inside the braces.

    • The gee part does a double eval on the replacement string.

    • The "qq|\\$1|" part is eval’d twice. The first eval replaces $1 into the string, and the second performs the interpolation.

    I cannot think of a two character combination here that would be a security breach…

    This method does not deal with the following properly:

    • Quoted strings. For example, Perl would not unescape the string ‘line 1\nline 2’ because of the single quotes.

    • Escapes sequences that are longer than a single character, such as hex \x1b or Unicode such as \N{U+...} or control sequences such as \cD

    • Anchored escapes, such as \LMAKE LOWER CASE\E or \Umake upper case\E

    If you want more complete escape replacement, you can use this regex:

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    print "\n\n\n\n";
    
    binmode STDOUT, ":utf8";
    
    while (my $data = <DATA>) {
        $data=~s/\\(
            (?:[arnt'"\\]) |               # Single char escapes
            (?:[ul].) |                    # uc or lc next char
            (?:x[0-9a-fA-F]{2}) |          # 2 digit hex escape
            (?:x\{[0-9a-fA-F]+\}) |        # more than 2 digit hex
            (?:\d{2,3}) |                  # octal
            (?:N\{U\+[0-9a-fA-F]{2,4}\})   # unicode by hex
            )/"qq|\\$1|"/geex;  
        print $data;
    }
    
    __DATA__
    Hello!\nI\'d like to tell you a little \"secret\".
    Here is octal: \120 
    Here is UNICODE: \N{U+0041} and \N{U+41} and \N{U+263D}
    Here is a little hex:\x50 \x5fa \x{5fa} \x{263B}
    lower case next char \lU \lA
    upper case next char \ua \uu
    A backslask:\\
    Tab'\t'stop
    line 1\rline 2  (on Unix, "line 1" will get overwritten)
    line 3\\nline 4 (should result in "line 3\\nline 4")
    line 5\r\nline 6
    

    That handles all Perl escapes except:

    1. Anchored type (\Q, \U, \L ended by \E)

    2. Quoted forms, such as 'don't \n escape in single quotes' or [not \n in here]

    3. named unicode characters, such as \N{THAI CHARACTER SO SO}

    4. Control characters like \cD (that is easily added…)

    But that was not part of your question as I understood it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: How can I find the method that called the current method? I
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: .NET windows application, can it be compressed into a single .exe? To
Possible Duplicate: VS2008 Setup Project: Shared (By All Users) Application Data Files? Please can
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? How do you filter on
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.