import re
regex = re.compile(r"^(PANEL\s+\S+)(.*?)(?=PANEL|\Z)", flags=re.MULTILINE | re.DOTALL)
test_str = ("PANEL NORMAL\n"
"Diana and Gahriel run away with all their might. The monster is raging right behind them\n\n"
" Gharial (whisper)\n"
" Don’t turn back!\n\n"
" Diana (confused)\n"
" What--\n"
" \n"
"PANEL LEFT\n\n"
"Gahriel signal diane to be quiet\n\n"
" Gharial (put his hand to his mouth signal quiet)\n"
" Shh….\n\n"
"PANEL FOCUS\n\n"
"The monster is running toward them quickly , right behind them\n\n"
"PANEL FULL\n\n"
"Diana is afraid for her own life. Her eyes wide open as the monster breathes through her.\n\n"
" Diana (holding her mouth with her hand)\n"
" Gasp\n\n"
"PANEL RIGHT\n"
" Gahriel (whisper)\n"
" Everything will be okay!\n\n"
"PANEL CENTER\n"
"They both stand quietly, not a single noise.,\n\n"
"PANEL SMALL\n"
"A sudden noise\n"
"Crack\n\n"
"PANEL RIGHT\n"
"The monster caught Diana and bit her head off. Gahriel in shock-terror, he can’t move.\n\n")
subst = "<div class=\"$1\">$2</div>"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html