Python Multiline Regex
January 13, 2009 | categories: Python | View CommentsAfter 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:
import re pattern = re.compile('some\s*fancy', re.DOTALL) match = pattern.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!
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!
September 1st, 2010 at 1:47 pm
Many thanks!
Good example, good explanation.