I'm searching through a text file for a certain string then looking to find another string following that string, it could be on the next line or further down the document. I currently have
so an example text output would like
there is a word1. then there is some more text.
then we are looking for word2 = apple.
i'm looking to return the word 'apple' + word1. However word2= can be on the next line or further down the document. i've managed to do the below but this only works if its on the next line. not if it was on line 3,4, 5 etc. can anyone help?
if 'word1' in line and 'word2' not in line:
nextLine = next(f)
pattern = re.match('(?:word2=|word2 =)([a-z0-9_])+',nextLine)
if pattern:
print('word1', pattern)