I just wasted far too much time trying to work out why a regex would work for my example test cases but not with real data. Turns out “.”, DOT_ALL, and newlines are more complicated than I realised.

My problem essentially came down to not knowing that the IMAP messages I was working with used CRLF and that my OS/shell/applications were silently converting text to LF when I was constructing my test cases.

nikic has a good post covering many details with PCRE and newlines