> I'm wondering what I'm doing wrong,
\W is a negation of \w but is still including \n
I negated both with [^\w\n]
That's the result you wanted? Can't really comment on the rest, looks weird to me.
#!/usr/bin/env perl
use strict;
use warnings;
my $book = do {local $/; <DATA>}; # slurp the book
# Split book into words (delimited by \b{wb}), sequences of newlines,
# and sequences of anything else.
while ($book =~
/(
( [^\w\n]+ )
|
( \b{wb}.+?\b{wb} )
|
( \n+ )
)
/xg) {
show($1);
}
print "\n";
# show(): make spaces and newlines visible
sub show {
my $str = shift;
$str =~ tr/\n/$/;
$str =~ tr/ /_/;
print "{$str}\n";
}
__DATA__
--First paragraph--
Second one's followed by only one newline. "Hello," she said, "How's t
+ricks?"
Third paragraph doesn't end with any punctuation ... and the splitting
+ works
4th one is separated by two newlines.
The End.
OUTPUT: {--}
{First}
{_}
{paragraph}
{--}
{$}
{Second}
{_}
{one's}
{_}
{followed}
{_}
{by}
{_}
{only}
{_}
{one}
{_}
{newline}
{._"}
{Hello}
{,"_}
{she}
{_}
{said}
{,_"}
{How's}
{_}
{tricks}
{?"}
{$$}
{Third}
{_}
{paragraph}
{_}
{doesn't}
{_}
{end}
{_}
{with}
{_}
{any}
{_}
{punctuation}
{_..._}
{and}
{_}
{the}
{_}
{splitting}
{_}
{works}
{$$}
{4th}
{_}
{one}
{_}
{is}
{_}
{separated}
{_}
{by}
{_}
{two}
{_}
{newlines}
{.}
{$$}
{_________}
{The}
{_}
{End}
{.}
{$}
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.
|
|