fork download
  1. import re
  2. string_example = "my name is (Nimrod) and my last name is (Rappaport)"
  3. pattern = r'my name is (.+) and my last name is (.+)'
  4. regx = re.compile(pattern)
  5. caps = []
  6. uncaps = []
  7. for m in regx.finditer(string_example):
  8. n_caps = []
  9. n_uncaps = []
  10. start_from = 0
  11. for group_id in range(regx.groups):
  12. n_caps.append(m.group(group_id+1))
  13. n_uncaps.append(m.group()[start_from:m.start(group_id+1)-m.start()])
  14. start_from = m.end(group_id+1)-m.start()
  15. if start_from < len(m.group()):
  16. n_uncaps.append(m.group()[start_from:])
  17. caps.append(n_caps)
  18. uncaps.append(n_uncaps)
  19.  
  20. print(caps)
  21. print(uncaps)
  22.  
  23.  
  24.  
Success #stdin #stdout 0.02s 9464KB
stdin
Standard input is empty
stdout
[['(Nimrod)', '(Rappaport)']]
[['my name is ', ' and my last name is ']]