while ($arrayElement =~ /:ID\s(\d*).*:P_ID\s(\d*)/g ) { print $1,$2,"\n"; # just an example }