regexperlgrammarregexp-grammars

How do I match a multiline pattern using Regexp::Grammars?


I'm new to Regexp::Grammars, and am having trouble matching a multi-line pattern. I have this input:

my $text = <<EOD;
HEADER:
This is a multi-line section, because the
second line is down here.

EOD

and this grammar:

use Regexp::Grammars;
my $parser = qr{
  <nocontext:>
  <doc>
  <rule: doc>           <[section]>+
  <rule: section>       <label> : <text> (\n\n | $)
  <token: label>        [A-Z0-9_&/ -]+
  <token: text>         [^\n]*
}xms;

I'm only matching the first line of the section, but I'd like to capture all text up to a blank line or end of input. Can anyone see what I'm doing wrong?


Solution

  • One solution is to change <text> as follows:

    <token: text>         (?:(?!\n\n).)*
    

    This matches 0 or more characters that are not a newline followed by another newline. It's probably not the best possible solution, but it works.