fork download
  1. import re
  2. text="""Some random text here
  3.  
  4. Dear Shareholders We
  5. are pleased to provide you with this semiannual report for Fund for the six-month period ended April 30, 2018. For additional information about the Fund, please visit our website a, where you can access quarterly commentaries. We value the trust that you place in us and look forward to serving your investment needs in the years to come.
  6. Best regards
  7. Douglas
  8.  
  9. Random text here as well"""
  10. letter_begin = ["dear", "to our", "estimated"] # All expressions for "beginning" of letter
  11. openings = "|".join(letter_begin)
  12. letter_end = ["sincerely", "yours", "best regards"] # All expressions for "ending" of Letter
  13. closings = "|".join(letter_end)
  14. regex = r"(?:{})[\s\S]*?(?:{}).*(?:\n.*){{0,2}}".format(openings, closings)
  15. print(regex)
  16. print(re.findall(regex, text, re.IGNORECASE))
Success #stdin #stdout 0.02s 27728KB
stdin
Standard input is empty
stdout
(?:dear|to our|estimated)[\s\S]*?(?:sincerely|yours|best regards).*(?:\n.*){0,2}
['Dear Shareholders We\nare pleased to provide you with this semiannual report for Fund for the six-month period ended April 30, 2018. For additional information about the Fund, please visit our website a, where you can access quarterly commentaries. We value the trust that you place in us and look forward to serving your investment needs in the years to come.\nBest regards \nDouglas\n']