Попробуйте:
use strict;
use warnings;
use Data::Dumper;
my $str = 'mg_delay_1_2_5_25_30_it';
my $start = 'mg_delay';
my $end = 'it';
if (my ($res) = $str =~ /\Q$start\E_((?:\d+_)+)\Q$end\E/) {
my @items = $res =~ /(\d+)/g;
my @versions = map { $start . '_' . $_ . '_' . $end } @items;
print Dumper(\@versions);
}
Вывод :
$VAR1 = [
'mg_delay_1_it',
'mg_delay_2_it',
'mg_delay_5_it',
'mg_delay_25_it',
'mg_delay_30_it'
];
В качестве альтернативы, если $start
и $end
не известны:
my $str = 'mg_delay_1_2_5_25_30_it';
if (my ($start, $res, $end ) = $str =~ /^((?:(?!_\d).)+)_((?:\d+_)+)(.+)$/) {
my @items = $res =~ /(\d+)/g;
my @versions = map { $start . '_' . $_ . '_' . $end } @items;
print Dumper(\@versions);
}