I have a long list of strings which contain substrings of interest in the order they are given, but here is a small example using sentences in a text file:
This is a long drawn out sentence needed to emphasize a topic I am trying to learn.
It is new idea for me and I need your help with it please!
Thank you so much in advance, I really appreciate it.
From this text file, I would like to find any sentences that contain both "I"
and "need"
but they must occur in that order.
So in this example, 'I'
and 'need'
both occur in sentence 1 and sentence 2 but in sentence 1 they are in the wrong order, so I do not want to return that. I only want to return the second sentence, as it has 'I need'
in order.
I have used this example to identify the substrings, but I cannot figure out how to only find them in order:
id1 = "I"
id2 = "need"
with open('fun.txt') as f:
for line in f:
if id1 and id2 in line:
print(line[:-1])
This returns:
This is a long drawn out sentence needed to emphasize a topic I am trying to learn.
It is new idea for me and I need your help with it please!
But I want only:
It is new idea for me and I need your help with it please!
Thanks!