fork download
  1. import re
  2.  
  3. regex = r"has\(\s*(\d+)\s*,\s*(\d+)\s*\)"
  4.  
  5. dict={}
  6. test_str = ("Person1(has(1, 1) has(2, 2)\n"
  7. " has(3, 3) \n"
  8. " had(4, 4) had(5, 5))\n"
  9. "Person2(had(6, 6) has(7, 7))\n"
  10. "Person3(had(6, 6) has(8, 8))")
  11.  
  12. res=re.split(r"(Person\d+)",test_str)
  13. currentKey="";
  14. for rs in res:
  15. if "Person" in rs:
  16. currentKey=rs;
  17. elif currentKey !="":
  18. matches = re.finditer(regex, rs, re.DOTALL)
  19. ar=[]
  20. for match in matches:
  21. ar.append(match.group(1)+","+match.group(2))
  22. dict[currentKey]=ar;
  23. print(dict)
  24.  
  25.  
  26.  
  27.  
Success #stdin #stdout 0.02s 27752KB
stdin
Standard input is empty
stdout
{'Person1': ['1,1', '2,2', '3,3'], 'Person2': ['7,7'], 'Person3': ['8,8']}