在正则表达式组内可选匹配
我正在使用正则表达式来解析nginx日志。在正则表达式组内可选匹配
我创建的一个领域是'消息'。有时这个组将包含一个JSON字符串。
如何在我的'消息'组中选择匹配以创建'json'组?
这里是我当前的正则表达式:
(?<time>\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) \[(?<log_level>\w+)\] (?<pid>\d+).(?<tid>\d+): (?<message>.*)
下面是一个例子消息:
2017/12/02 12:45:40 [debug] 29#29: *7895 [lua] auth_client.lua:41: {"applicationName":"nginx", "applicationFunction":"auth-client" ,"logLevel": "DEBUG", "clientId":"xyz","requestId":"72e03798e9826f2c4b076e3779903edf","message":"Checking client credentials"}
以我目前的正则表达式我得到一个领域,如:
message: *7895 [lua] auth_client.lua:41: {"applicationName":"nginx-auth-filter", "applicationFunction":"auth-client" ,"logLevel": "DEBUG", "clientId":"ifly","requestId":"72e03798e9826f2c4b076e3779903edf","message":"Checking client credentials"}
我想是什么:
message: *7895 [lua] auth_client.lua:41: {"applicationName":"nginx-auth-filter", "applicationFunction":"auth-client" ,"logLevel": "DEBUG", "clientId":"ifly","requestId":"72e03798e9826f2c4b076e3779903edf","message":"Checking client credentials"} json: {"applicationName":"nginx-auth-filter", "applicationFunction":"auth-client" ,"logLevel": "DEBUG", "clientId":"ifly","requestId":"72e03798e9826f2c4b076e3779903edf","message":"Checking client credentials"}
但是,有时消息不包含json,因此该组应该是可选的。
回答:
你可以使用这个表达式与可选组JSON字符串:
(?<time>\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) \[(?<log_level>\w+)\] (?<pid>\d+).(?<tid>\d+): (?<message>[^{]*(?<json>\{.*\})?.*)
这是假设JSON文本与第一{
开始,并以邮件组中最后}
结束。
RegEx Demo
以上是 在正则表达式组内可选匹配 的全部内容, 来源链接: utcz.com/qa/259585.html