# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(PANEL\s+\S+)(.*?)(?=PANEL|\Z)"
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>"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE | re.DOTALL)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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