Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Regex with newline

by voyager (Friar)
on Apr 04, 2001 at 23:01 UTC ( #69798=perlquestion: print w/replies, xml ) Need Help??

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'.
(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

    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 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

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.

    $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!


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 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;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://69798]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-05-21 12:26 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.