fork download
  1. import re
  2. p = re.compile(r'^(text[ ]*=[ ]*\')([^\']*)(\')')
  3. strs = ["text = 'a,b,c'", "text = 'a'", "text = ''"]
  4. print([p.sub(lambda x: x.group(1) + (x.group(2) + ",d" if x.group(2) else "d" ) + x.group(3), s) for s in strs])
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
["text = 'a,b,c,d'", "text = 'a,d'", "text = 'd'"]