2013-04-11 50 views
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 

選擇最後出現我需要的圖案在「發送請求」和之間,以提取具有「唐納德」請求「消息被髮送IS」。在上面的例子中的兩個請求中包含「唐納德」 。所以重複列表應該有2個項目。圍繞比賽的一部分模式使用python

回答

1

認沽括號要查找:

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

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

產生

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 

不使用括號(定義一組),findall剛剛返回整個字符串,因爲整個字符串匹配模式。

docs explain

返回所有非重疊的字符串模式的匹配,作爲 字符串列表。字符串從左到右掃描,匹配返回 找到的順序。如果該模式中存在一個或多個組,則 會返回組列表。

0

由於需要最後一次出現:

.+Sending request(.+)TO BE SENT IS.*?$