import res = "text\n!if\ntext1\n!endif\ntext2"s = re.sub("^!if$.*?^!endif$\s+", "", s, flags=re.S | re.M)print s
Standard input is empty
text text2
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!