s="""04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: jockey
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 82
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: Donald
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 83
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 [STACK]( 1653): Not doing anything
04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: Mickey
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 84
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS
04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: Donald
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 83
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS
04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: jockey
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 82
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS"""
exepat= re.compile(".*Sending request.*?Donald.*?TO BE SENT IS",re.DOTALL)
reout = exepat.findall(s)
print reout[0]
Expected Output:
04-09 11:11:57.879 D/PTT [STACK]( 1653): *********Sending request
04-09 11:11:57.879 [STACK]( 1653): *********
04-09 11:11:57.879 [STACK]( 1653): S: abcd
04-09 11:11:57.879 [STACK]( 1653): l: Donald
04-09 11:11:57.879 [STACK]( 1653): k: sucess
04-09 11:11:57.879 [STACK]( 1653): j: 83
04-09 11:11:57.879 [STACK]( 1653):
04-09 11:11:57.879 [STACK]( 1653): MESSAGE TO BE SENT IS
I need a pattern to extract requests which have "Donald" in between "Sending request" and "MESSAGE TO BE SENT IS".In the above example two requests contains "Donald".So reout list should have 2 items.