Let's look at this text:
foo bar
foo bar
bar foo
bla
blu
WORD foo bar
foo bar
I'd like to have a python re.search
expressions that will match the line starts with WORD
and all the non-blank lines before.
So from the text above, it should extract
bla
blu
WORD foo bar
My non-working approach is:
re.search(r'\n\n.*(?!\n\n)WORD.*?\n', text, flags=re.DOTALL)
Found the answer myself:
re.search(r'\n(.(?!\n\n))*\nWORD.*?\n', text, flags=re.DOTALL)
Searches for a line starting with WORD
and includes that line with all other non-blank lines.