Unable to make regular expression match on multiline files
fredrik at pythonware.com
Fri Nov 16 00:15:25 CET 2001
David Lees wrote:
> I am trying to use a simple regular expression to extract some digits
> which are tagged with ascii text. Everything works fine on a single
> line, but when I use text that has the '\n' character it fails. Here is
> a sample.
> >>> p=re.compile('.*Number States: (\d+)',re.MULTILINE)
you want DOTALL, not MULTILINE:
>>> import re
>>> p = re.compile(".*Number States: (\d+)", re.DOTALL)
>>> a = " End: -1STATUS\nNumber States: 6\njunk"
<SRE_Match object at 008D6798>
> >>> a=' End: -1STATS:\nNumber States: 6\njunk'
> >>> m=p.match(a)
> >>> m.groups()
or better, use re.search instead of re.match:
>>> p = re.compile("Number States: (\d+)")
<SRE_Match object at 008D6AF8>
<!-- (the eff-bot guide to) the python standard library:
More information about the Python-list