import res = "aba\naaa\naba\n\naba"print( re.sub(r'(?:[\r\n]+.*b.*)+$|^.*b.*', '', s, flags=re.M).replace('\n','\\n') )
Standard input is empty
\naaa
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!