Regular expression help

Tony Meyer ta-meyer at ihug.co.nz
Fri Jul 18 06:49:58 CEST 2003


> > How about re.findall?
> >     >>> re.findall('BEGIN(.*?)END', 'BEGIN foo END   BEGIN 
> bar END') 
> >     [' foo ', ' bar ']
> 
> 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 ']

You need the re.DOTALL flag.  I don't think you can pass this to the findall
function, but you can if you compile the re, eg:

>>> import re
>>> r = re.compile('BEGIN(.*?)END', re.DOTALL)
>>> r.findall('BEGIN foo\nmumble END   BEGIN bar END')
[' foo\nmumble ', ' bar ']

=Tony Meyer






More information about the Python-list mailing list