fork download
  1. import re
  2.  
  3. s = "This is a sentence which contains keyword1. And keyword2 is inside this sentence. "
  4.  
  5. pattern = re.compile(r"([A-Z][^.!?]*(?:(keyword1)|(keyword2))[^.!?]*[.!?])\s")
  6. for match in pattern.findall(s):
  7. print(match)
  8.  
Success #stdin #stdout 0.03s 9580KB
stdin
Standard input is empty
stdout
('This is a sentence which contains keyword1.', 'keyword1', '')
('And keyword2 is inside this sentence.', '', 'keyword2')