fork download
  1. import re
  2.  
  3. regex = r"^\s*\[[^][]*\](?:\r?\n(?![^\S\r\n]*\[]$).*)*\r?\n[^\S\r\n]*\[]$\s*"
  4. test_str = ("Hi this is my config file.\n"
  5. "Please dont delete it\n\n"
  6. "[homes]\n"
  7. " browseable = No\n"
  8. " comment = Your Home\n"
  9. " create mode = 0640\n"
  10. " csc policy = disable\n"
  11. " directory mask = 0750\n"
  12. " public = No\n"
  13. " writeable = Yes\n\n"
  14. "[proj]\n"
  15. " browseable = Yes\n"
  16. " comment = Project directories\n"
  17. " csc policy = disable\n"
  18. " path = /proj\n"
  19. " public = No\n"
  20. " writeable = Yes\n\n"
  21. " []\n\n"
  22. "This last second line.\n"
  23. "End of the line.")
  24.  
  25. result = re.sub(regex, "", test_str, 0, re.MULTILINE)
  26. print(result)
Success #stdin #stdout 0.02s 9456KB
stdin
Standard input is empty
stdout
Hi this is my config file.
Please dont delete it
This last second line.
End of the line.