Python multiline regex
Posted by Mike on Jan 13th, 2009
After pulling most of my hair out, I realized a simple mistake in my Python regular expression usage. I had been trying to search a simple HTML document for some text, which happened to be broken across a newline such as:
<div>I'm some fancy text that needs to be found</div>
I tried my very simple pattern match using the re.MULTILINE flag to no avail. The solution ended up being:
p = re.compile('some\s*fancy', re.DOTALL) p.search(input_text)
re.MULTILINE is not the same as re.DOTALL. Use DOTALL if you want your dot to match everything, including new lines. See more at this handy dandy reference site.
Doh!
- Filed under software development
- Tagged with python
- Comments(3)

February 14th, 2010 at 11:34 am
Thanks, Mike. I was trying re.MULTILINE over and over wondering what I was missing.
And google found your post. Bless you.
March 15th, 2010 at 3:39 pm
Ditto. Thanks!
March 27th, 2010 at 8:20 am
many thanks guy!