$ perle 'my $x = "1 12 123"; while ($x =~ /(\d+)\s*/g) { say $1 }' 1 12 123 $ perle 'my $x = "1_12_123"; while ($x =~ /(\d+)\s*/g) { say $1 }' 1 12 123 $ perle 'my $x = "1_12_123"; while ($x =~ /(\d+)/g) { say $1 }' 1 12 123