my @match; if ($str =~ /^foo /) { while ($str =~ /m (\d+) /g) { push @match, $1; } }