import re
text = """subject name="Biology" value="12th"
topic name="system" value="anything"
topic
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"
topic
subject name="ComputerSc" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic"""
l=['Biology','chem102']
remove_topic = False
for line in text.splitlines():
if line.startswith("subject"):
name = re.search(r'\bname="([^"]*)"', line)
if name:
if name.group(1) in l:
remove_topic = True
print(line)
elif line.strip() == "topic" and remove_topic:
print()
remove_topic = False
else:
print(line)
aW1wb3J0IHJlCnRleHQgPSAiIiJzdWJqZWN0IG5hbWU9IkJpb2xvZ3kiIHZhbHVlPSIxMnRoIgp0b3BpYyBuYW1lPSJzeXN0ZW0iIHZhbHVlPSJhbnl0aGluZyIKdG9waWMKc3ViamVjdCBuYW1lPSJtYXRoZW1hdGljcyIgdmFsdWU9IjEydGgiCnRvcGljIG5hbWU9InN5c3RlbSIgdmFsdWU9ImFueXRoaW5nIgpvdGhlciB0b3BpY3MgPSJhbnl0aGluZyIgdmFsdWU9ImFueXRoaW5nIgp0b3BpYwpzdWJqZWN0IG5hbWU9ImNoZW0xMDIiIHZhbHVlPSIxMnRoIgp0b3BpYyBuYW1lPSJzeXN0ZW0iIHZhbHVlPSJhbnl0aGluZyIKdG9waWMKc3ViamVjdCBuYW1lPSJDb21wdXRlclNjIiB2YWx1ZT0iMTJ0aCIKdG9waWMgbmFtZT0ic3lzdGVtIiB2YWx1ZT0iYW55dGhpbmciCm90aGVyIHRvcGljcyA9ImFueXRoaW5nIiB2YWx1ZT0iYW55dGhpbmciCnRvcGljIiIiCgpsPVsnQmlvbG9neScsJ2NoZW0xMDInXQpyZW1vdmVfdG9waWMgPSBGYWxzZQoKZm9yIGxpbmUgaW4gdGV4dC5zcGxpdGxpbmVzKCk6CglpZiBsaW5lLnN0YXJ0c3dpdGgoInN1YmplY3QiKToKCQluYW1lID0gcmUuc2VhcmNoKHInXGJuYW1lPSIoW14iXSopIicsIGxpbmUpCgkJaWYgbmFtZToKCQkJaWYgbmFtZS5ncm91cCgxKSBpbiBsOgoJCQkJcmVtb3ZlX3RvcGljID0gVHJ1ZQoJCXByaW50KGxpbmUpCgllbGlmIGxpbmUuc3RyaXAoKSA9PSAidG9waWMiIGFuZCByZW1vdmVfdG9waWM6CgkJcHJpbnQoKQoJCXJlbW92ZV90b3BpYyA9IEZhbHNlCgllbHNlOgoJCXByaW50KGxpbmUp