mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to match multiple lines of text using a regular expression. i.e.
body: Here is an example of a multi-line section of text that I'm trying to grab. attachment:
My code doesn't work:
if ($line =~ /^body: (.*)^attachment:/mi) { $vars{body} = $1; }
Can someone offer a solution?

Replies are listed 'Best First'.
Re: Multiline regex
by haoess (Curate) on Oct 27, 2006 at 18:36 UTC

    If you want to do your matching about the whole string (containing multiple lines), then you want /s (also let the dot meta character match line breaks).

    my ($body) = $line =~ /^body: \n (.*) \n attachment/xsi;

    Or you are matching line by line, then you should have a look at the flip-flop operator in perlop.

    # this is from perlop while (<>) { $in_header = 1 .. /^$/; $in_body = /^$/ .. eof; if ($in_header) { # ... } else { # in body # ... } }

    --Frank

      Also see Flipin good, or a total flop? for a good discussion of the flip-flop operator by Grandfather.



      --chargrill
      s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Multiline regex
by smokemachine (Hermit) on Nov 08, 2006 at 00:46 UTC
    my ($body) = $line =~ /^body:\s+(.*)\s+^attachment:/msi