Python regex, matching pattern over multiple lines.. why isn't this working?

Solution 1:

Multiline doesn't mean . will match line return, it means that ^ and $ are limited to lines only

re.M re.MULTILINE

When specified, the pattern character '^' matches at the beginning of the string and at the >beginning of each line (immediately following each newline); and the pattern character '$' >matches at the end of the string and at the end of each line (immediately preceding each >newline). By default, '^' matches only at the beginning of the string, and '$' only at the >end of the string and immediately before the newline (if any) at the end of the string.

re.S or re.DOTALL makes . match even new lines.

Source

http://docs.python.org/

Solution 2:

Try re.findall(r"####(.*?)\s(.*?)\s####", string, re.DOTALL) (works with re.compile too, of course).

This regexp will return tuples containing the number of the section and the section content.

For your example, this will return [('1', 'ttteest'), ('2', ' \n\nttest')].

(BTW: your example won't run, for multiline strings, use ''' or """)