I'd like to iterate over each of a m//g style regex's FULL matches,
even
though it contains capturing groups.  Then, for each FULL match, I'd
like to examine the capturing groups that are local to that match.

The thing I'm struggling with is that, while it's easy to iterate over
full matches when a regex does not contain capturing groups, the water
becomes muddy when you add capturing groups to the mix.  See my little
test example below:

use strict; use warnings;

my $subject = qq(ab x cd y ef z);
my $re1 = qr/\w\w/;
my $re2 = qr/(\w)(\w)/;
my @a1 = $subject =~ m/$re1/g;
my @a2 = $subject =~ m/$re2/g;

# prints each match;
# output =
# re1 match #1 = ab
# re1 match #2 = cd
# re1 match #3 = ef
my $matchcount = 0;
for my $match (@a1) {
    $matchcount++;
    print "re1 match #" . $matchcount . " = " . $match . "\n";
}

# we're no longer iterating over matches, we're iterating over
capturing
# groups. this is fine in many cases, but not what I need sometimes;
# output =
# re2 match #1 = a
# re2 match #2 = b
# re2 match #3 = c
# re2 match #4 = d
# re2 match #5 = e
# re2 match #6 = f
$matchcount = 0;
for my $match (@a2) {
    $matchcount++;
    print "re2 match #" . $matchcount . " = " . $match . "\n";
}

# Instead of the above, I'd like to iterate over $re2's FULL matches,
# even though it contains capturing groups.  Then, for each FULL
match,
# I'd like to examine the capturing groups local to that match.
# conceptually, it would work something like this: (pseudocode):

=pod
 my $matchcount = 0;
 my $groupcount = 0;
 # reality check -- matches() function doesn't exist!
 for my $match (matches(@a2)) {
     $matchcount++;
     print "re2 match #" . $matchcount . " = " . $match . "\n";
     # reality check -- groups() function doesn't exist!
     for my $group (groups($match)) {
         print "\t group #" . $groupcount . " = " . $group . "\n";
     }
 }
=cut

# the theoretical output of this would look like this:
# re2 match #1 = ab
#       group #1 = a
#       group #2 = b
# re2 match #2 = cd
#       group #1 = c
#       group #2 = d
# re2 match #3 = ef
#       group #1 = e
#       group #2 = f


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to