You didn't mention how you're reading in the document. It may be necessary, in additon to using the /s modifier on your substitution, to also slurp the entire file at once. Otherwise, you'll probably just be reading it one line at a time, and that could foul up your matching.