grok filter(regex)在方括号中提取字符串

时间:2015-06-25 04:25:14

标签: regex pattern-matching logstash-grok square-bracket

我的应用程序日志条目如下:

2015-06-24 14:03:16.7288  Sent request message [649b85fa-bfa0-4cb4-8c38-1aeacd1cbf74] <Request>sometext</Request>

2015-06-24 14:38:05.2460  Received response message [649b85fa-bfa0-4cb4-8c38-1aeacd1cbf74] <Response>sometext</Response>

我正在使用logstash grok过滤器来使用方括号提取xml内容和客户端令牌。

grok {  
    match => ["message", "(?<content>(<Request(.)*?</Request>))"]   
    match => ["message", "(?<clienttoken>(Sent request message \[(.)*?\]))"]
    add_tag => "Request"
    break_on_match => false
    tag_on_failure => [ ]
}

grok {  
    match => ["message", "(?<content>(<Response(.)*?</Response>))"] 
    match => ["message", "(?<clienttoken>(Received response message \[(.)*?\]))"]
    add_tag => "Response"
    break_on_match => false
    tag_on_failure => [ ]
}

现在结果如下所示

对于第一个日志行:

Content =  <Request>sometext</Request>
clienttoken = Sent request message [649b85fa-bfa0-4cb4-8c38-1aeacd1cbf74]

对于第二个日志行:

Content = <Response>sometext</Response>
clienttoken = Received response message [649b85fa-bfa0-4cb4-8c38-1aeacd1cbf74]

但我希望结果如下:

Content = <Request>sometext</Request>
clienttoken = 649b85fa-bfa0-4cb4-8c38-1aeacd1cbf74

请告诉我如何只提取方括号内的字符串,而不会在模式中找到所有匹配的字符串。

1 个答案:

答案 0 :(得分:3)

您可以使用lookbehind和lookahead断言。

(?<=Sent request message \[).*?(?=\])

同样对响应消息也这样做。