in reply to execute loop code for every occurence of regex

Update: Note to self, don't come to PM first thing in the morning. I missed the "must match pair" part of the question. Just change .* to .*? in your regexp and ignore what follows in this post. It's still neat reading, though :)

\|i([^|]*).*\|l([^|]* means match everything from the first |i barcode to the last |l homeloc, inclusively. Try replacing your while with the one below:

$line = '|i1234|lABCD|i5678|i90|lBOO'; while($line =~ /\|i([^|]*)|\|l([^|]*)/g) { print("barcode: $1\n") if (defined($1)); print("homeloc: $2\n") if (defined($2)); } __END__ output ====== barcode: 1234 homeloc: ABCD barcode: 5678 barcode: 90 homeloc: BOO

Here's another nice solution that uses m/\G.../gc

$line = '|i1234|lABCD|i5678|i90|lBOO'; for ($line) { /\G \|i ([^|]*) /gcx && do { print "barcode: $1\n"; redo; }; /\G \|l ([^|]*) /gcx && do { print "homeloc: $1\n"; redo; }; /\G \|[^il] [^|]* /gcx && do { redo; }; # skip bad stuff } __END__ output ====== barcode: 1234 homeloc: ABCD barcode: 5678 barcode: 90 homeloc: BOO