The problem is that your while loop is iterating line-by-line, but you are attempting a multi-line match. Your regex never sees more than one line at a time. In order to do a multi-line match you'll have to slurp the whole file into a scalar and then use the 's' modifier for the regex.