fork download
  1. import re
  2.  
  3. xml = """<?xml version="1.0" encoding="utf-8" ?>
  4. <book_list>
  5. <book rbr="1" >
  6. <title> Yacc </title>
  7. <author> Filip Maric </author>
  8. <year> 2004 </year>
  9. <publisher> Matematicki fakultet </publisher>
  10. <price currency="din"> 100 </price>
  11. </book>
  12. <book rbr="2" >
  13. <author> Fredrik Lundh </author>
  14. <price currency="eur"> 50 </price>
  15. <publisher> O’Reilly & Associates </publisher>
  16. <year> 2001 </year>
  17. <title> Python Standard Library </title>
  18. </book>
  19. </book_list>"""
  20.  
  21. m = re.search(r'<book\s*rbr="\d+"\s*>(?!.*</book>.*<title> Python Standard Library </title>).*(?P<book><title> Python Standard Library </title>).*?</book>', xml, flags=re.DOTALL)
  22. print(m.group('book'))
  23. m = re.search(r'<book\s*rbr="\d+"\s*>(?!.*</book>.*<title> Yacc </title>).*(?P<book><title> Yacc </title>).*?</book>', xml, flags=re.DOTALL)
  24. print(m.group('book'))
  25.  
Success #stdin #stdout 0.02s 9588KB
stdin
Standard input is empty
stdout
<title> Python Standard Library </title>
<title> Yacc </title>