How to find start and end index of all matches of a word in a string with Python

1 Answer

0 votes
import re     

s = 'xyxxxyyyxy'     
word = 'xy'

for match in re.finditer(word, s):         
    st = match.start()         
    en = match.end()         
    print('Found from index {} to index {}'.format(st, en))



'''
run:

Found from index 0 to index 2
Found from index 4 to index 6
Found from index 8 to index 10

'''

 



answered Apr 27, 2019 by avibootz
...