[Python-bugs-list] [ python-Bugs-675864 ] patch for new func. string.findall

SourceForge.net noreply@sourceforge.net
Mon, 27 Jan 2003 18:00:38 -0800


Bugs item #675864, was opened at 2003-01-27 21:00
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=675864&group_id=5470

Category: Extension Modules
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Hunter Peress (hfastedge)
Assigned to: Nobody/Anonymous (nobody)
Summary: patch for new func. string.findall

Initial Comment:
this finds all indices of a substring in a string
returning in list form. Its not in pydoc2.3.

This "patch" contains unittests.

#!/usr/bin/python
import 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'


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=675864&group_id=5470