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

Eli Billauer eli at billauer.co.il
Wed Dec 31 13:41:38 PST 2008


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:


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):


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,


Web: http://www.billauer.co.il

More information about the Perl mailing list