patch for string.findall (why not, theres an re.findall) 
Author Message
 patch for string.findall (why not, theres an re.findall)

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'



Fri, 15 Jul 2005 09:06:09 GMT  
 patch for string.findall (why not, theres an re.findall)
Quote:

> 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

[...]

If you need an index, use re.finditer() instead of findall.

Inyeol...



Sat, 16 Jul 2005 02:41:12 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. findall - why a list of groups not matches?

2. Where is /pat/g (Perl) and findall() (Python )?

3. Where is /pat/g (Perl) and findall() (Python)?

4. Regular Expression: Bug in findall?

5. AttributeError in re.findAll ?

6. Bug in re.findall()?

7. Way to return a dictionary from re.findall?

8. re findall mod for issue of side effects

9. Alex Martinelli's solution to regular expression/findall problem

10. Problem with re.findall?????!!!!!!!!!!

11. HELP findall please???

12. findall???

 

 
Powered by phpBB® Forum Software