foreach my $lines(@temparray) #where temparray is the above { chomp $lines; $string=$string.$lines; } while($string=~/(\w+ )([\w\W\d]+?\};)+?/g) { print "$1$2
"; } #### while($string=~/(\w+ )([\w\W\d]+?\};\n)+?/gm)