fork download
  1. import re
  2.  
  3. def successive_matches(pattern,text,pos=0):
  4. ptrn = re.compile(pattern)
  5. match = ptrn.match(text,pos)
  6. while match:
  7. yield match.group()
  8. if match.end() == pos:
  9. break
  10. pos = match.end()
  11. match = ptrn.match(text,pos)
  12. if pos < len(text) - 1:
  13. yield text[pos:]
  14.  
  15. for matched_text in successive_matches(r"('[^']*'|[^',]*),\s*","21, 2, '23.5R25 ETADT', 'description, with a comma'"):
  16. print matched_text
  17.  
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
21, 
2, 
'23.5R25 ETADT', 
'description, with a comma'