我要尋找一個較短的版本代碼perl的分配正則表達式匹配組while循環
my $s = 'test123abc456qwe789ghj999';
while($s =~ /(?<g1>\d+)[^\d]+(?<g2>\d+)/g) {
my ($g1, $g2) = ($1, $2);
#do s.th. with g1 and g2
}
我嘗試這樣做的,但它會導致死循環
my $s = 'test123abc456qwe789ghj999';
while(my ($g1, $g2) = ($s =~ /(?<g1>\d+)[^\d]+(?<g2>\d+)/g)) {
#do s.th. with g1 and g2
}
還是有可能在while循環中獲得匹配作爲散列?
(
g1 => 123,
g2 => 456
)
(
g1 => 789,
g2 => 999
)
您的匹配已經被捕獲在'%+'哈希中。例如,你可以做一些類似'say $ + {g1} * $ + {g2};' –