http://qs321.pair.com?node_id=749612


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