We help IT Professionals succeed at work.

python findall position of matches

Hi,

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?

Comment
Watch Question

Did you look at
http://docs.python.org/library/re.html#match-objects

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):
    pos=0
    while True:
        match = regex.search(searchstring, pos)
        if not match:
             return
        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

Author

Commented:
many thanks!