patch for string.findall (why not, theres an re.findall)
Hunter Peress
hu.peress at mail.mcgill.ca
Sun Jan 26 20:06:09 EST 2003
this finds all indices of a given substring in a string.
and yes, i already checked pydoc2.3 string
Heres code+unittests.
Im running this by the group for any extra input before i submit it to sf
#!/usr/bin/python
import sys,commands,os,re,string
def findall(sub,s):
if s =='' or sub =='':
return []
ret=[]
findval=0
pos=0
while findval != -1:
findval = s.find(sub,pos)
if findval != -1:
ret.append(findval)
pos = findval + len(sub)
return ret
if __name__ == '__main__':
units = [
'asdsad','l',
'l','asdlsds',
'l','lsdlsds',
'l','lsdsds',
'l','sdsds',
'l','sdsdsl',
'l','lsdsdsl',
'l','lsdlsdsl',
'l','l',
'l','ll',
'l','lll',
'lo','llollol',
'lo','llollolo',
'','',
'','asdasd',
'asdsad',''
]
for i in range(0,len(units),2):
sub,s = units[i],units[i+1]
print "'%s' in '%s':"%(sub,s)
print findall(sub,s)
print "-"*30
print 'done'
More information about the Python-list
mailing list