2

I'm running the example from slide 15:

qr{
  <data>
  <rule: data>    <[text]>+
  <rule: text>    .+
}xm;

When running against a multi-line text:

line_1
line_2

I get:

'text' => [ 'line-1',
            '
            line-2' ]

and so far I've not succeeded getting rid of the '\n' in front of the second line captured.

Running Regexp::Grammers 1.048 on top of Strawberry perl 5.26.1.

update / clarification Having (pre-maturely - sorry!) raised a bug against the module, Damian clarified as follows (reply slightly adapted to match above example):

A rule with whitespace within it matches any whitespace (including newlines) in the input at that point. So a rule like:

<rule: text>    .+

is really equivalent to:

<rule: text><.ws>.+

meaning: match-but-don't-capture any leading whitespace, then match any-characters-except-newline.

If you want whitespace inside the rule to be ignored (as you seem to want here), then you need to declare the rule as a token instead. Tokens don't have the magical "whitespace-matches-whitespace" behaviour of rules. Hence you would write:

<token: line> .+

in which case you will also need to explicitly consume the newlines separating each line, with something like:

<rule: data> <[line]>+ % \n

1 Answer 1

1

This works:

qr{
  <data>
  <rule: data>  <[text]>+ % [\r\n]+
  <rule: text>  .+
}xm;

The lines of data are meant to be separated by EOL character(s) which the

[\r\n]+

specifies. Note: some Windows files end each line with both a new line \n and a line feed \r character hence the [\r\n]+ pattern. You can read more about this by doing a perldoc Regexp::Grammars and searching for separator

5
  • Thx., Ken; I wasn't thinking of the separator. I find it more natural to write <rule: data> <[text]>+ % $. I'm still not clear though why in the original case . matched \n (as the regex runs under modifier /xm ... - any hint appreciated.
    – Stefan_E
    Jan 4, 2018 at 2:00
  • In my test case I added a \n new line before the "line 1" text and it did NOT have the '\n' included in the result! After seeing that (confused) and after looking into Damian's code (complicated) it appeared to me that something like a separator option was going to be needed because what I thought would fix the problem (i.e. <rule: text> [^\r\n]+ ) ... did not. The perldoc is very long but fortunately for me I searched for "sep" and found the solution in short order. Steven probably should fix his slide 15 ... Jan 4, 2018 at 5:58
  • Thx., Ken; IMHO, this is sufficiently inconsistent that I dared to file a bug report over at CPAN: link
    – Stefan_E
    Jan 4, 2018 at 15:46
  • Thx. do Damian's reply, clarified and resolved question at hand. See above for update.
    – Stefan_E
    Jan 5, 2018 at 18:33
  • Damian is the best! I've been a fan for years and have bought his books. Jan 10, 2018 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.