Isn't re.findall supposed to find all?

Rodrigo Senra rodsenra at correionet.com.br
Sat Nov 11 03:43:18 CET 2000


June Kim wrote:
> 
> Well, I forgot the old rule "Longest match possible."
> It worked perfectly with this:
> >>> z
> '[abcdefrdofhd]kdeioslkdfj[sdkfj]'
> >>> p=re.compile('\[[^\[]*\]')
> >>> re.findall(p,z)
> ['[abcdefrdofhd]', '[sdkfj]']


How about this ?
>>> import re
>>> p = re.compile(r'(\[\w*\])')
>>> print p.findall(z)
['[abcdefrdofhd]', '[sdkfj]']

> z='this is yet another start of a sting end but never start an end'
> 
> opening bracket : "start"
> closing bracket : "end"
> 
> What I want to get is,
> 
> re.findall(p,z) == ['[start of a string end]','[start an end]']
> 
> How should I render the regular expression?

Try this:
>>> z='this is yet another start of a sting end but never start an end'

Non-greedy version:
>>> p = re.compile(r'(start[\s\w]*?end)')
>>> print p.findall(z)
['start of a sting end', 'start an end']


Greedy:
>>> p = re.compile(r'(start[\s\w]*end)')
>>> print p.findall(z)
['start of a sting end but never start an end']
HTH
Rod
-- 
Rodrigo Senra         
Computer Engineer   (GPr Sistemas Ltda)  rodsenra at correionet.com.br 
MSc Student of Reflection (IC- UNICAMP) Rodrigo.Senra at ic.unicamp.br
http://www.ic.unicamp.br/~921234 (see also   http://www.gpr.com.br)





More information about the Python-list mailing list