#!/usr/bin/perl -w use strict; my @args; my $string="garbage validcommand 1234 221 garbage 999 213"; while ($string =~ m!validcommand\s+(\d+(?:\s+\d+)*)!g) { push(@args, $1); } print @args;