$_ = ...; # file contents my $min_depth = 4; while (/([A-Z_]+ [a-z_0-9]+ = \{([^\}]*\{){$min_depth,}[^;]*;)/g) { my $array = $1; .... }