fork download
  1. import re
  2.  
  3. a = [
  4. "Mr: Smith",
  5. "Mr: Smith; John",
  6. "Smith",
  7. "Smith; John"
  8. ]
  9. r = r"^(?:([^:]+):\W*)?([^;]+)(?:;\W*(.+))?"
  10.  
  11. def repl(m):
  12. return (m.group(1) or "title" ) + "," + m.group(2) + "," + (m.group(3) or "fname")
  13.  
  14. for s in a:
  15. print re.sub(r, repl, s)
Success #stdin #stdout 0.02s 6920KB
stdin
Standard input is empty
stdout
Mr,Smith,fname
Mr,Smith,John
title,Smith,fname
title,Smith,John