简体   繁体   中英

Find start and end positions of all occurrences within a string in Python

If you have a sequence:

example='abcdefabcdefabcdefg'

and your searching for:

searching_for='abc'

what function would give you a list with all the positions?

positions=[(0,2),(6-8),(12-14)]

i created a window list that splits 'example' by 3 so it goes from 'abc','bcd','cde'

windows=['abc', 'bcd', 'cde', 'def', 'efa', 'fab', 'abc', 'bcd', 'cde', 'def', 'efa', 'fab', 'abc', 'bcd', 'cde', 'def']

and used a for loop

for i in windows:
    if i == 'abc':

thats where i get stuck . . .

You can use regular expressions ; the match objects come with position information attached . Example using Python 2:

>>> import re
>>> example = 'abcdefabcdefabcdefg'
>>> for match in re.finditer('abc', example):
        print match.start(), match.end()
0 3
6 9 
12 15

The re module provides what you need.

import re
print [(m.start(0), m.end(0)) for m in re.finditer('abc', 'abcdefabcdefabcdefg')]

This is elegantly expressed by a list comprehension:

positions = [(i, i + len(searching_for) - 1)
             for i in xrange(len(example))
             if example[i:].startswith(searching_for)]

Note that it's often more useful to have the end index point after the last character, rather than to the last character as you asked for (and the above code provides).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM