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