while ( ) { chomp; my ( $number, $time ) = m{ (\d\.\d{3}) # a number with 3 decimal places .*? , \s* # some characters, a comma and some spaces (\d\d:\d\d:\d\d) # HH:MM:SS }x; print "$number,$time\n" if defined $number && defined $time; } __DATA__ 1.4567 ,11:00:00 1.4571 ,11:00:01 , 1.4567 ,11:58:00rftft 1.4566 , 1.5555 ,11:43:00