[Tutor] Python re without string consumption

Terry Carroll carroll at tjc.com
Fri Jan 26 01:44:29 CET 2007


On Thu, 25 Jan 2007, Jacob Abraham wrote:

>    I would like to thank you for the solution and
> the helper funtion that I have written is as follows. 

That's very similar to a solution I coded up for a friend, who was doing 
searches in genetic sequences, and had exactly the same problem  you did.  
My solution:

def myfindall(regex, seq):
   resultlist=[]
   pos=0

   while True:
      result = regex.search(seq, pos)
      if result is None:
         break
      resultlist.append(seq[result.start():result.end()])
      pos = result.start()+1
   return resultlist

Using it:

>>> rexp=re.compile("B.B")
>>> sequence="BABBEBIB"
>>> print myfindall(rexp,sequence)
['BAB', 'BEB', 'BIB']

> But I do hope that future versions of Python include a regular
> expression syntax to handle such cases...

My tongue-in-cheek recommendation was that re.findall be renamed to 
re.findmost.





More information about the Tutor mailing list