Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Splitting multiline string into words, the stuff between words, and newlines

by LanX (Saint)
on Feb 24, 2022 at 00:55 UTC ( [id://11141604]=note: print w/replies, xml ) Need Help??


in reply to Splitting multiline string into words, the stuff between words, and newlines

> 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} {.} {$}

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Splitting multiline string into words, the stuff between words, and newlines
by ibm1620 (Hermit) on Feb 24, 2022 at 01:21 UTC
    Yes, that's exactly what I wanted. Thank you.

    (I'm just playing with different ways of building Markov chains, a la dissociated-press.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141604]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-24 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found