fork(10) download
  1. import re
  2.  
  3. string = "It was never going to work, he thought. He did not play so well, so he had to practice some more. Not foobar !"
  4.  
  5. transformed = re.sub(r'(?:not|never|no)[\w\s]+[^\w\s]',
  6. lambda match: re.sub(r'(\s+)(\w+)', r'\1NEG_\2', match.group(0)),
  7. string,
  8. flags=re.IGNORECASE)
  9.  
  10. print transformed
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
It was never NEG_going NEG_to NEG_work, he thought. He did not NEG_play NEG_so NEG_well, so he had to practice some more. Not NEG_foobar !