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.


#!/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.