my $str = '012346.jpg 012346-all.jpg 012346-EXP.jpg'; while ($str =~ /(\d+)(.*?\.jpg)/gi) { print "$1$2\n"; }