while ($text =~ /(#+(?:.|\s)+?)sub +([\w\s]+) +\{((.|\s)+)/) { ... } # :)