fork(1) download
  1. import re
  2. subject = 'I have a _cat, here is a pic {cat_pic}. Another_pic {cat_figure}'
  3. regex = re.compile(r'{[^}]*}|(_)')
  4. def myreplacement(m):
  5. if m.group(1):
  6. return ""
  7. else:
  8. return m.group(0)
  9. replaced = regex.sub(myreplacement, subject)
  10. print(replaced)
  11.  
Success #stdin #stdout 0.03s 9440KB
stdin
Standard input is empty
stdout
I have a cat, here is a pic {cat_pic}. Anotherpic {cat_figure}