HTTP-404 has asked for the wisdom of the Perl Monks concerning the following question:

Hello I made newsgroup downloader, that saves new posts in html file, but they are screwed up, at first i thought it would be easy to replace new line char with br but, but it didn't work for me
$post=$c->body($first_article); $post=~m/^\n$/<br>; print $post;
this code doesn't replace them, btw i use NNTPClient from CPAN Thank You very much in advance

Replies are listed 'Best First'.
Re: Replace new line char with br
by dvergin (Monsignor) on Aug 19, 2001 at 08:25 UTC
    $post =~ s/\n/<br>\n/g;

    ought to do the job. Or (depending on where you like your <br>'s):

    $post =~ s/\n/\n<br>/g;

    Note that I'm assuming that you would like to retain the new-lines so the contents of $post remain easily readable. If not, just drop the   \n   on the right side of the substitution.

    There may be some folks who will be made nervous by the match on \n. If so, just remember that the infamous   /s   and   /m   qualifiers only affect matches with '.' on the one hand and ^ and $ on the other. \n matches mid-stream new-lines without any help from modifiers.

    Update: See my answer to gryphon for the spiffier solution:

          $post =~ s/$/<br>/mg;

Re: Replace new line char with br
by I0 (Priest) on Aug 19, 2001 at 08:27 UTC
    $post=~s/\n/<br>/g;
(ichimunki) Re: Replace new line char with br
by ichimunki (Priest) on Aug 19, 2001 at 16:43 UTC
    Okay. No one actually explains why your regex doesn't work, so I will take a stab at it.

    1) If you want to do a substitution the form is $post =~ s/matching bit/replacement bits/modifier bits; (note the s instead of the m- s is for substitute, m is for match)

    2) When you start a matching expression with ^ it will match the beginning of the line. $ is for the end of the line. Neither of these are necessary to match a new line character, really. Since the character you are looking for defines where lines begin and end.

    3) You probably want to put a modifier or two at the end of your expression to make sure that you match and substitute all of the \n's in your $post. I suggest /sg. Adding s will make the RE treat your entire $post as one string. Adding g will make the RE test and match until it can't find anymore matches-- otherwise it will only match/substitute once.

    So just to clarify, I suggest using $post =~ s/\n/<br>/sg; . And don't be afraid to read the regex docs at perldoc perlre.
Re: Replace new line char with br
by gryphon (Abbot) on Aug 19, 2001 at 10:12 UTC

    Greetings HTTP-404,

    I don't know what OS you're using, but if you're using Win32, you may need to compensate for the line feed added character. The following might help:

    $post =~ s/^\r*\n$/<br>/g;

    -gryphon
    code('Perl') || die;

      gryphon, you have a couple of oversights in your regex.

      HTTP-404 appears to have multiple new-lines in $post. But your use of $ in your regex will only find the last one. To make the $ work for mid-string new-lines, you would need to use the /m modifier. And since you are attempting to use \n (and perhaps \r) for your match, the $ is superfluous to your strategy anyway.

      Also, Perl automagically hides the difference between OS's handling of new-lines. I can match \n on my windows machine with no problems. Perl pretends it's a single character and hides the ugly details from me. To put it another way; \n is an OS-compliant newline, not linefeed. So you don't need to worry about \r. Trying to match it will confuse things.

      Finally, if we wanted to depend on $ for our solution, we might go for: $post =~ s/$/<br>/mg; which is sweeter than the regex in my original post.

Re: Replace new line char with br
by Monky Python (Scribe) on Aug 19, 2001 at 12:56 UTC
    just another way to do it...

    $post=~s/.$/<BR>/g;

    MP

      Considering that $ can match before or after a newline, and a regular expression will match the first place in a string that it can, this will replace the last character before the newline with the tag. Which isn't what you wanted to do...
        of course you're right.... copy and paste are sometimes not your friends.....

        MP