Reputation: 735
Is there a way to use capture groups in more than binding expression and capture all the groups?
#!/usr/bin/perl
use strict;
use warnings;
countDays(1,"2015-3-21","2016-3-24");
sub countDays {
die "Check formatting"
unless ($_[0] =~ m/([1-7])/ &&
$_[1] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})$/ &&
$_[2] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})$/);
# testing
print "$1\n$2\n$3\n$4\n$5\n$6\n$6\n";
}
This only captures the last three groups: $1
, $2
, and $3
.
Edit for expected output as Avinash Raj suggested:
1
2015
3
21
2016
3
24
Upvotes: 0
Views: 88
Reputation: 133
#!/usr/bin/env perl
use strict;
use warnings;
countDays(1,"2015-3-21","2016-3-24");
sub countDays {
my $countDays = join ',', @_;
die "Check formatting"
unless $countDays =~
m/([1-7]),(\d{4})-(\d{1,2})-(\d{1,2}),(\d{4})-(\d{1,2})-(\d{1,2})/;
# testing
print "$1\n$2\n$3\n$4\n$5\n$6\n$7\n";
}
Upvotes: 1
Reputation: 85757
No, every successful match resets all capture variables. But you can do this:
sub countDays {
my @match1 = $_[0] =~ m/([1-7])/
and
my @match2 = $_[1] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})$/
and
my @match3 = $_[2] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})$/
or die "Check formatting";
print "@match1\n@match2\n@match3\n";
}
Upvotes: 6