0
    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

我需要一种模式来提取在“发送请求”和“要发送的消息是”之间具有“唐纳德”的请求。在上面的示例中,两个请求包含“唐纳德”。所以 reout 列表应该有 2 个项目。

4

2 回答 2

1

Put parentheses around the part of the match you want to find:

exepat= re.compile(".*Sending request(.*)TO BE SENT IS", re.DOTALL)

for reout in exepat.findall(s):
    print(reout)

yields

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: 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 

Without parentheses (defining a group), findall is just returning the whole string because the whole string matches the pattern.

The docs explain:

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups.

于 2013-04-11T09:16:02.810 回答
0

由于您需要最后一次出现:

.+Sending request(.+)TO BE SENT IS.*?$ 
于 2013-04-11T09:26:18.320 回答