$templaty = qr/ \[% # open delim ( (?!%\]) # not-a-closer lookahead . # anything else )* # as many as there are %\] # close delim /x; @vars = ($string =~ $templaty);