2017-06-04 17 views
2

的我有這樣的日誌消息:Logstash神交比賽進行到最後一個索引unti開始的UserAgent

"sid-cmascioieiow89322&New*Sou,th%20Skvn%20and%20ir&o,n%20Age,Mozilla/5.0 (Linux; Android 6.0; CHM-U01 Build/HonorCHM-U01) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36" 

和模式:

"(?[^&])&(?[^,]),%{GREEDYDATA:User_Agent}" 

問題是P2有時包含零或一個或一個以上逗號。我想匹配UserAgent之前的最後一個逗號,因爲UserAgent有一段時間包含逗號。

這是神交調試器鏈接:https://grokdebug.herokuapp.com/

現在:

{ 
    "p1": [ 
     "sid-cmascioieiow89322" 
    ], 
    "p2": [ 
     "New*Sou" 
    ], 
    "User_Agent": [ 
     "th%20Skvn%20and%20iro,n%20Age,Mozilla/5.0 (Linux; Android 6.0; CHM-U01 Build/HonorCHM-U01) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36" 
    ] 
} 

我想是這樣的:

{ 
    "p1": [ 
     "sid-cmascioieiow89322" 
    ], 
    "p2": [ 
     "New*Sou,th%20Skvn%20and%20ir&o,n%20Age" 
    ], 
    "User_Agent": [ 
     "Mozilla/5.0 (Linux; Android 6.0; CHM-U01 Build/HonorCHM-U01) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36" 
    ] 
} 

謝謝您的幫助。

+0

是否用戶代理之前'p2'總是包含,除空白字符的字符串?嘗試使用'(? \ S *)' –

+0

很高興爲您工作。 –

回答

0

要捕獲到p2部分的字符串部分沒有空格。因此,而不是匹配,其他任何零個或多個字符,你可以使用[^,]*模式\S* - 任何0+ 非空白字符儘可能多的,因此\S*,將匹配逗號即是最後的一連串非空白字符。

(?<p1>[^&]*)&(?<p2>\S*),%{GREEDYDATA:User_Agent} 
      ^^^^^^^^^^ 

This is how this regex matches您的日誌數據:enter image description here

見神交演示截圖: enter image description here