Regular expression help

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Jul 18 01:07:40 EDT 2003


On Fri, Jul 18, 2003 at 04:31:32AM +0000, David Lees wrote:
> Andrew Bennetts wrote:
> >
> >How about re.findall?

[...]

> Actually this fails with the multi-line type of file I was asking about.
> 
> >>> re.findall('BEGIN(.*?)END', 'BEGIN foo\nmumble END   BEGIN bar END')
> [' bar ']

Oh!  So it does.  I presumed it took an optional flags argument, like
re.search, but it doesn't.  Compiling the pattern with re.M doesn't seem to
help (and I can't figure out how to include the flag inline -- "(?M)"
doesn't seem to work).

On the other hand, this hack does work:

>>> re.findall('BEGIN((?:.|\\n)*?)END', 'BEGIN foo\nmumble END   BEGIN bar END')
[' foo\nmumble ', ' bar ']

(There's probably a more elegant way to do that, but I'm not a re expert)

-Andrew.






More information about the Python-list mailing list