Regular expression help

Bengt Richter bokr at oz.net
Fri Jul 18 00:53:43 EDT 2003


On Fri, 18 Jul 2003 04:31:32 GMT, David Lees <abcdebl2nonspammy at verizon.net> wrote:

>Andrew Bennetts wrote:
>> On Thu, Jul 17, 2003 at 04:27:23AM +0000, David Lees wrote:
>> 
>>>I forget how to find multiple instances of stuff between tags using 
>>>regular expressions.  Specifically I want to find all the text between a 
>> 
>>                                                ^^^^^^^^
>> 
>> How about re.findall?
>> 
>> E.g.:
>> 
>>     >>> re.findall('BEGIN(.*?)END', 'BEGIN foo END   BEGIN bar END') 
>>     [' foo ', ' bar ']
>> 
>> -Andrew.
>> 
>> 
>
>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 ']
>
It works if you include the DOTALL flag (?s) at the beginning, which makes
. also match \n: (BTW, (?si) would make it case-insensitive).

 >>> import re
 >>> re.findall('(?s)BEGIN(.*?)END', 'BEGIN foo\nmumble END   BEGIN bar END')
 [' foo\nmumble ', ' bar ']

Regards,
Bengt Richter




More information about the Python-list mailing list