Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Regexing a block of text in between patterns

by mwah (Hermit)
on Mar 10, 2009 at 14:39 UTC ( [id://749612]=note: print w/replies, xml ) Need Help??


in reply to Regexing a block of text in between patterns

You could split on 'Sidenote':

... my $filename = 'C:\letters.txt'; open my $fh, '<', $filename or die "Can't read $filename, $!"; my $text = do { local $/; <$fh> }; close $fh; my @sidenotes = split /(?=\[Sidenote:)/, $text; my $div_output = join "\n", map "<div>$_</div>", @sidenotes; print $div_output; ...

Regards

mwa

Replies are listed 'Best First'.
Re^2: Regexing a block of text in between patterns
by CountZero (Bishop) on Mar 10, 2009 at 17:15 UTC
    Or rather
    my @sidenotes = split /\[Sidenote:.*?\]/, $text;
    Otherwise you will leave some junk at the beginning of each block.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hello CountZero

      Otherwise you will leave some junk at the beginning of each block

      I purposely included the [Sidenote] text after understanding the OP's specification this way.

      Maybe I didn't read closely enough ...

      Thanks & Regards

      mwa

        Now that you say it I looked again at his regex and indeed one could think that "[Sidenote:" is the breakpoint, which does not fully tally with his question.

        Well, he now has both options to choose from!

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found