It's not specifically part of your question;
however, it occurs to me that you might not want to capture all that excess leading and trailing whitespace.
Compare these:
$ perl -E '
my $re = qr{(\w+)((?:\s*\d+\s*)*)};
my $str = "a 1 2 3 b 4 5 6";
while ($str =~ /$re/g) {
say "|$&|: |$1| |$2|";
}
'
|a 1 2 3 |: |a| | 1 2 3 |
|b 4 5 6|: |b| | 4 5 6|
$ perl -E '
my $re = qr{(\w+)\s+((?:\s*\d+)*)};
my $str = "a 1 2 3 b 4 5 6";
while ($str =~ /$re/g) {
say "|$&|: |$1| |$2|";
}
'
|a 1 2 3|: |a| |1 2 3|
|b 4 5 6|: |b| |4 5 6|