fork download
  1. import re
  2. reg = re.compile(r"(\btext *= *)'([^']*)'")
  3.  
  4. def repl(m):
  5. if len(m.group(2)) == 0:
  6. return m.group(1) + "'d'";
  7. else:
  8. return m.group(1) + "'" + m.group(2) + ",d'"
  9.  
  10.  
  11. print(reg.sub(repl, r"text = 'a,b,c'"))
  12. print(reg.sub(repl, r"text = ''"))
  13. print(reg.sub(repl, r"text = 'a'"))
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
text = 'a,b,c,d'
text = 'd'
text = 'a,d'