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