The first reply addresses doing this right way, I'll go ahead and show you how to do it the wrong way, using line matching like you tried. We'll achieve the same effect though.
#!/usr/bin/perl -l
use strict;
use warnings;
my $file;
{
local $/ = undef;
$file = scalar <DATA>;
}
## Find me two newlines or be the start of the file
## Followed by any amount of newlines
## Followed by one or more characters (our capture, your paragraph)
## Followed by any amount of newlines
## Followed by any two newlines or the end of the file
## Flags: treat the file as one line, continue search for more paragra
+phs (/g in list context)
while ( $file =~ m/ (?>\n{2}|\A) \n* (.+?) \n* (?=\n{2}|\z)/sxg ) {
print "<p>$1</p>";
}
__DATA__
Paragraph one is this
Paragraph two is this
Paragraph three is this
Paragraph three and this
But stops here and doesn't get the rest of the newlines
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.