fork download
  1. import re
  2.  
  3. mystring = """
  4.  
  5. # catch this comment
  6. !mycommand
  7. # catch this comment
  8. #catch this comment too
  9. !mycommand
  10.  
  11. # catch this comment
  12. !mycommand
  13. !mycommand
  14.  
  15. some code. match until the previous line
  16. # do not catch this comment
  17. !mycommand
  18. # do not catch this comment
  19. """
  20.  
  21. import re
  22. m = re.search(r'^(?:\s*(?:#.*|!mycommand\s*))+\s*', mystring)
  23. print(m.group())
  24.  
Success #stdin #stdout 0.02s 9524KB
stdin
Standard input is empty
stdout

# catch this comment
!mycommand
# catch this comment
#catch this comment too
!mycommand

# catch this comment
!mycommand
!mycommand