fork download
  1. import re
  2. text = """subject name="Biology" value="12th"
  3. topic name="system" value="anything"
  4. topic
  5. subject name="mathematics" value="12th"
  6. topic name="system" value="anything"
  7. other topics ="anything" value="anything"
  8. topic
  9. subject name="chem102" value="12th"
  10. topic name="system" value="anything"
  11. topic
  12. subject name="ComputerSc" value="12th"
  13. topic name="system" value="anything"
  14. other topics ="anything" value="anything"
  15. topic"""
  16.  
  17. l=['Biology','chem102']
  18. remove_topic = False
  19.  
  20. for line in text.splitlines():
  21. if line.startswith("subject"):
  22. name = re.search(r'\bname="([^"]*)"', line)
  23. if name:
  24. if name.group(1) in l:
  25. remove_topic = True
  26. print(line)
  27. elif line.strip() == "topic" and remove_topic:
  28. print()
  29. remove_topic = False
  30. else:
  31. print(line)
Success #stdin #stdout 0.02s 9592KB
stdin
Standard input is empty
stdout
subject name="Biology" value="12th"
topic name="system" value="anything"

subject name="mathematics" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic
subject name="chem102" value="12th"
topic name="system" value="anything"

subject name="ComputerSc" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic