Perl捕获多个绑定运算符

时间:2015-08-15 15:14:02

标签: regex perl capture-group

有没有办法在多个绑定表达式中使用捕获组并捕获所有组?

#!/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";

}

这仅捕获最后三组:$1$2$3

根据Avinash Raj的建议编辑预期输出:

1
2015
3
21
2016
3
24

2 个答案:

答案 0 :(得分:6)

不,每次成功匹配都会重置所有捕获变量。但你可以这样做:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
    var cancelSearchBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelBarButtonItemClicked")
    self.navigationItem.setRightBarButtonItem(cancelSearchBarButtonItem, animated: true)
    return true
}

答案 1 :(得分:1)

SpecificVersion