fork download
  1. import re
  2.  
  3. x = 'john got shot dead. john with his .... ? , john got killed or died in 1990. john with his wife dead or died'
  4.  
  5. x = re.sub(r'[^\w]', ' ', x) # removed all dots, commas, special symbols
  6.  
  7. for i in re.findall(r'(?<=\bjohn\b)(?:(?!\bjohn\b).)*?(?=\b(?:dead|died|death)\b)', x):
  8. print i.strip()
  9. print len([word for word in i.split()])
  10.  
  11.  
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
got shot
2
got killed or
3
with his wife
3