Some secondary points:
in reply to Grepping with Perl: How to stop after first match?
- I think # is not the best delimiter.
- dot in a default regex does not match a newline, so you probably don't need the last \n in there (Update: if you make the dot-star greedy)
- Did you slurp the file? If not, what is your input record separator? Depending on the structure of the file and whether you are doing other matches with the data, you may be able to make use of the input record separator to simplify your regex.
- I would recommend an if block, but if you want to use and for flow control, consider putting the and at beginning of the second line
What is the purpose of (.*?)