hmbscully has asked for the wisdom of the Perl Monks concerning the following question:

Say I have this line of input from a file:
|decoraw@act.org|This is my message.\\nI hope these returns will work +this time.\\nWendy|http://www.act.org|3|3|
And this perl script:
#!/usr/bin/perl $mailprog = '/usr/sbin/sendmail -t'; $admin_email="decoraw\@act.org"; $subject="Testing the new mass mailer"; $file = "/actapps/suitespot/cgi-bin/sender/returntest3.txt"; $number_sent = 0; open(DATA, "$file") || die "Can't open $file"; # open the file for +reading while($line = <DATA>) { chomp $line; ($junk,$email,$message,$survey_url,$login,$password) = split(/\|/, +$line); $message =~ s|\\n|\n|g; #replace the escaped newlines with newlin +es for formatting open (MAIL, "|$mailprog -t") || die "Can't open $mailprog! \n"; print MAIL "Content-type:text/plain\n"; print MAIL "From: $admin_email\n"; print MAIL "To: $email\n"; print MAIL "Subject: $subject\n"; print MAIL "$message\n\n"; print MAIL "URL: $survey_url\n"; print MAIL "Login: $login\n"; print MAIL "Password: $password\n"; close(MAIL); $number_sent++; }
And I get this output in an email:
This is my message.\ I hope these returns will work this time.\ Wendy URL: http://www.act.org Login: 3 Password: 3
What is wrong with my regex  $message =~ s|\\n|\n|g;  #replace the escaped newlines with newlines for formatting that is causing the new lines to be inserted but one \ to remain? I've tried several variations on the regex, but this is the only one i've been able to actually get the newlines showup properly. TIA -Wendy

Replies are listed 'Best First'.
Re: escaping newlines, regex question
by Cine (Friar) on Aug 23, 2001 at 22:55 UTC
    You should use s|\\\\n|\n|g instead

    T I M T O W T D I
      That's it!! Thank you so very much. -Wendy
        The reason is not because your regex was bad. The reason was because the text file has two literal backslashes and then an "n". The file should only have one literal backslash -- having two is kind of silly. Had the file been:
        this|that|a long\ndescription\nhere
        (where each character is a literal character), then s/\\n/\n/g would have worked.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;