We help IT Professionals succeed at work.

python findall position of matches


I'm using re.findall to search for patterns in an entire file. Is there a way I can determine the position of the matches in the file?

Watch Question

Did you look at

Each match object supports the methods start() and stop()
My mistake:

findall() does not return match objects, but just strings.
so my above solution is not working.

You had to make your own findall(), that returns match objects

def myfindall(regex, searchstring):
    while True:
        match = regex.search(searchstring, pos)
        if not match:
        yield match
        pos = match.end() 

myre = re.compile(r'\d+')

mystring = 'ads 123 asdas 123 dsada 22'

for match in myfindall(myre, mystring):
    print 'found at %2d: <%s>' % (match.start(), match.group())

Open in new window

It's not my day today.

I overlooked re.finditer()

for match in re.finditer(myre, mystring):
    print 'found at %2d: <%s>' % (match.start(), match.group())

# or if you want to convert the iterator as fast as possible to a list:

allmatches = list(re.finditer(myre, mystring) )

for match in allmatches:
    print 'found at %2d: <%s>' % (match.start(), match.group())

Open in new window


many thanks!