[Israel.pm] Regex: Groups inside a ?:-cluster

Gaal Yahas gaal at forum2.org
Wed Dec 31 14:34:07 PST 2008


The docs for @- in perlvar state:

> "$-["n"]" is the offset of the start of the substring matched by n-th subpattern, or
> undef if the subpattern did not match. [...]  $n coincides with "substr $_, $-[n],
> $+[n] - $-[n]" if "$-[n]" is defined

Note "did not match", not "doesn't match"; as you say your third
pattern never ran (indeed, why should it). But note also that the doc
isn't clear on what $n is specced to be when $-[n] is not defined.

I couldn't find other mention of this, so I'd say this behavior is a
bit underspecced, but unlikely to change in Perl 5 -- too many things
would break otherwise.


On Wed, Dec 31, 2008 at 11:41 PM, Eli Billauer <eli at billauer.co.il> wrote:
> Hello,
>
>
> This little riddle is a result of my discovery of the qr//, even though
> I won't use it here. I show a simplified case below. The idea is to have
> the regular expression spit out certain pieces from the scanned string.
> This is the script:
>
>
> ----------------------------------------
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $s = "x0 yb";
>
> my @z = ($s =~ /(?:x(\d)|y(.)|.([012].))/gi);
>
> # Following line substitutes undefined entries in the array
> # with the string "(undefined)"
>
> @z = map { defined($_) ? $_ : "(undefined)" } @z;
>
> print join ",", @z; print "\n";
>
> ------------------------------------------
>
>
> The idea behind this script, is that I want it to scan the string (in
> $s) and give me the digit immediately after an "x" or the character
> after an "x". And there's also a third pattern, which happens to match
> "x0" as well (with the space after it).
>
>
> Note that the regular expression is a (?: ) cluster which doesn't, in
> itself, put any elements in the resulting list. But each of the three
> possibilities in the cluster has a ()-group, which do.
>
>
> When I ran this script, this was printed (perl v5.6.1 and v5.8.8):
>
>
> 0,(undefined),(undefined),(undefined),b,(undefined)
>
> So it turns out that each of the groups sent something to the list:
> Those that matched sent the expected string, and those who didn't sent
> an undef. Most interesting is the third element in the list, which is
> undef even though the pattern matches; It's undef because it was never
> tested (I suppose).
>
>
> Is this behavior reliable? Documented? Removing the undefs from the list
> is a piece of cake, but can I be sure that only the first expression
> will spit out data?
>
>
> Thanks in advance,
>
>    Eli
>
> --
> Web: http://www.billauer.co.il
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list