The split technique, as pointed out above, is certainly much more compact, but you now have to check explicitly that the last two elements are numeric:
if (my @match = split /\s+/) {
if ($match[-2] =~ /^\d{1,3}$/ and $match[-1] !~ /\D/) {
$match[-2] = $count++;
}
$_ = "@match\n";
}
(Note that it's easier to say that the last element doesn't match a non-digit...). Also, with an array, it doesn't matter if you capture one more or less; the code will adapt to the change.
Otherwise, if for some reason you needed to stay with the regular expression as posted (for instance, because you really didn't want to capture the 12th element or something), the following pattern is equivalent (repetitive stuff in the middle omitted) to yours, and less noisy:
/([^ ]+)\s+([^ ]+)\s+([^ ]+)...\s+(\d{1,3})\s+(\d+)$/
that is, you don't need to group \s+. And I suspect you may want \S+ rather than [^ ]+:
/(\S+)\s+(\S+)\s+(\S+)\s+...\s+(\d{1,3})\s+(\d+)$/
You could also have Perl build the pattern for you, rather than mess with the fiddly details:
my $pat = join ( '\\s+', (
('(\\S+)' x 15),
'(\\d{1,3})',
'(\\d+)',
));
$pat = qr/\A$pat\z/;
• another intruder with the mooring in the heart of the Perl
|