renormalist has asked for the wisdom of the Perl Monks concerning the following question:
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:Example 2:#! /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(\%+); }
#! /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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 5.10 regexes with subpatterns
by demerphq (Chancellor) on Aug 12, 2007 at 09:53 UTC | |
|
Re: 5.10 regexes with subpatterns
by renormalist (Sexton) on Aug 10, 2007 at 12:16 UTC |