Article 59SN1 Printing index no of string occurrences in line

Printing index no of string occurrences in line

by
senorseh
from LinuxQuestions.org on (#59SN1)
Using Python scripting environment, I am trying to print string index for each string occurrence from a file, The code I have written if as follows:-
#!/usr/bin/env python
import re

file = open("file.txt", "r")
regex = re.compile(r'xy ')

for line in file:
preamble = regex.findall(line)
for word in preamble:
print(""+ str(word))

The output, I am getting is
xy
xy
.
.

But I want index no of each occurrence of pattern xy as [10, 28, 76] occurrence and every new line occurrence for pattern xy of file.txt should be printed in new line.latest?d=yIl2AUoC8zA latest?i=33uuWD503yY:8Qjmytw4ZIU:F7zBnMy latest?i=33uuWD503yY:8Qjmytw4ZIU:V_sGLiP latest?d=qj6IDK7rITs latest?i=33uuWD503yY:8Qjmytw4ZIU:gIN9vFw33uuWD503yY
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments