use warnings; use strict; while (){ my @matches; while (/%([^%]+)%/g){ push @matches, $1; } print join ' ', @matches; print "\n"; } __DATA__ %PN1% %DIMMM%%DIMINCH% __END__ PN1 DIMMM DIMINCH