Hi!

I try to use the new advanced 5.9.5 regex features. I have problems to understand the usefulness of DEFINE'ing sub-patterns, because inside of them another feature, capture buffers via %+, does not work.

This would make DEFINE less fun, because structuring the regex this way and at the same time loosing the ability to reach captured parts seems quite antagonistic. So I suspect, I'm doing something wrong.

Please see the following two examples. How can I access the matched animal in the second example?

Example 1:
#! /usr/local/bin/perl5.9.5 use v5.9.5; use Data::Dumper; my $re; $re = qr/The animal is: (?<animal>monkey|tiger|lion)/; my $string = 'The animal is: tiger'; if ($string =~ $re) { say "Successfully matched."; print Dumper(\%+); }
Example 2:
#! /usr/local/bin/perl5.9.5 use v5.9.5; use Data::Dumper; my $re; $re = qr/ (?(DEFINE) (?<phrase>The \s animal \s is: \s (?<animal>monkey|tiger|lion)) ) (?&phrase) /x; my $string = 'The animal is: tiger'; if ($string =~ $re) { say "Successfully matched."; print Dumper(\%+); print Dumper(\%-); }

"man perlre" seems to confirm me:

Note that capture buffers matched inside of recursion are not accessible after the recursion returns, so the extra layer of capturing buffers is necessary. Thus $+{NAME_PAT} would not be defined even though $+{NAME} would be [see example].

But I hope there is a way to solve it somehow.

Thanks.

In reply to 5.10 regexes with subpatterns by renormalist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.