Here is an example of the type of text file I am trying to search (named usefile):
DOCK onomatopoeia
DOCK blah blah
blah DOCK blah
DOCK
blah blah blah
onomatopoeia
blah blah blah
blah blah DOCK
DOCK blah blah
DOCK blah
onomatopoeia
I am using a finditer statement to find everything between DOCK and onomatopoeia as follows:
re.finditer(r'((dock)(.+?)(onomatopoeia))', usefile, re.I|re.DOTALL)
Obviously Dock is a much more common word than onomatopoeia and I only want to grab text between the first instance of Dock before onomatopoeia. The regex I am using above grabs text between the first instance of Dock and stops when it hits onomatopoeia, so I might get Dock Dock Dock Dock onomatopoeia when I really only wanted Dock onomatopoeia.
To be clear what I want from above is:
1. DOCK onomatopoeia
2. DOCK blah blah blah onomatopoeia
3. DOCK blah onomatopoeia
Is there a way to search for onomatopoeia and go UP to the first instance of Dock, or a better way to solve my problem?
Thanks!