$temp_=~ /\!(\S+?) #tablename \[(.*?)\] #table variables (?:\[(.*?)\] (?:\[(.*?)\] (?:\[(.*?)\])?)?)? \s+.*/x; #white space then rest of text. print Dumper ($1, $2, $3, $4, $5);