Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (19.9k points)

Given text I want to find words occurring before unknown

import re

text="the women marathon unknown introduced at the summer olympics los angeles usa and unknown won"  

items=re.finditer('unknown',text).  #as there are 2 unknown

for i in items:  

   print(i.start()) #to get index of 2 unknown

output is

19 

81

Now how to extract words occuring before the two unknowns separately?

For 1st unknown I should get the,women.

and for the second unknown I should get usa,and

1 Answer

0 votes
by (25.1k points)
edited by

You can use regular expressions to find the first two words before unknown like this.

import re

text = "the women marathon unknown introduced at the summer olympics los angeles usa and unknown won"

matches = re.finditer('(\S+\s+){2}(?=unknown)', text)

for m in matches:

   print(m.group())

To know more about this you can have a look at the following video:-

Browse Categories

...