m/^ (?: ( $list # dictionary joined with '|' | $ # if end of line then success, so don't pop array | (?{ pop @submatches }) (?!) # pop array and suddenly fail ) (?{ push @submatches, $^N }) # after every matched word, push it to array # $^N - last captured group )+ $/x;