http://qs321.pair.com?node_id=69798

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

I have user entering chunk of text in a textarea. For the most part this will just be typing. On display, I want to convert two newlines into two BR tags, to preserve the formating from the textarea. So this works fine:
my $text = get_text_from_db(..); $text =~ s|\n|<br />|sg;
But these don't (can someone tell me why?):
$text =~ s|\n\n|<br /><br />|sg; $text =~ s|\n{2}|<br /><br />|sg;
In the end I want to convert any two newlines to two BR tags, but only if they are not followed by another html tag. So I am hoping for something like:
$text =~ s|\n{2}(^<)|<br /><br />$1|sg;

Replies are listed 'Best First'.
Re: Regex with newline
by merlyn (Sage) on Apr 04, 2001 at 23:03 UTC
    You've probably got some hidden returns in there as well as newlines, so your newlines are not adjacent. That's the most common problem with this approach, and has been dealt with in the past by other postings here.

    -- Randal L. Schwartz, Perl hacker

(jptxs) Re: Regex with newline
by jptxs (Curate) on Apr 05, 2001 at 00:51 UTC
    try looking at these two nodes for more info:

    fixing what textareas do to input
    ASCII to HTML

    both are relevant to this. Also, remember that when you think of a good title for your question, put that title into the search box and search on it. You'll be surprised what you can find. =)

    Update: Please don't take this as a lecture. Asking questions like this one is what this whole section is about. I point out the option to search simply because itcan save *you* time and get you answers in terms that may help you rephrase the questions - which is sometimes all you need to do to get to the answers yourself. =)

    "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
      I am properly chastised.

      I want to share that one of the nodes pointed to above had a reference to HTML::FromText which is just what I needed.

      Update: For those who care about these things, I did notice that this module uses uppercase tags which the W3C xhtml validator does not like.

Re: Regex with newline
by thabenksta (Pilgrim) on Apr 04, 2001 at 23:22 UTC

    I have had similar problems, its besause it puts a hard return in with the newlines.

    Use:
    $string =~ s/\r\n/<br>/g; or $string =~ s/\n\r/<br>/g;
    not sure which.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      I just had a similar problem, and this solved it. Thanks a bunch, merlyn and thabenksta!

      --cs

Re: Regex with newline
by voyager (Friar) on Apr 04, 2001 at 23:25 UTC
    Update: Thanks for the help. It was the return character. This works:
    $text =~ s|\r\n\r\n|<br /><br />|sg;
      Note that thabenksta's example does not include the '/s'. You do not need it and you obscure its meaning and usefulness when you use it reflexively just because you are working with multiple lines.

      '/s' simply allows '.' to match line break characters. It has no other effect on the ability of your regex to work with multiple lines.

      I know this was an old post but it answered a question for me however it also brought up another one....is there any way to limit the number of
      tags to only two..that is, no matter how many times a user presses the enter key paragraphs are separated by two spaces(and no more than two). Thanks
        Yes. For example:
        $text =~ s%(?:\r\n){2,}%<br /><br />%g;