regex - 使用 Grok 进行模式匹配时如何考虑空值?
问题描述
我正在尝试读取Apache NiFi
使用中的日志数据,grok
但无法获取所需的输出。这是我的示例数据:
[2019-07-16 22:20:16] local.INFO: news.index {"mobile":"959404576540","message":Mozilla/5.0 (Linux; Android 8.0.0; ATU-L42 Build/HUAWEIATU-L42; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36 a/2.7.0}
我尝试使用以下表达式,但对我没有用。
%{SYSLOG5424SD}%{JAVACLASS}: %{JAVACLASS} {%{QS}:%{QS},%{QS}:%{QS}}
如何从上述数据中提取2019-07-16 22:20:16
, 959404576540
, ?Mozilla/5.0 (Linux; Android 8.0.0; ATU-L42 Build/HUAWEIATU-L42; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36 a/2.7.0
编辑:
@Emma您的答案有效,但如果我的数据包含任何空值,则它不起作用。例如:
[2019-07-16 22:20:16] local.INFO: news.index {"mobile":"8765453673","message":null}
解决方案
This expression,
\[([^]]+?)\](.*?)"mobile":"\s*(\d+)\s*"\s*,\s*"message"\s*:\s*([^}]*?)\s*\}
for instance might be a start to extract those data.
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
推荐阅读
- android - 在 MVP 中使用 Retrofit 测试 RxJava 调用被通缉但未被调用
- php - PHP 无法检查 Magento 管理员是否使用在根文件夹中工作的脚本登录子文件夹
- django - django中外键的反向关系
- php - 在 Laravel 中对用户列表进行排序
- android - Room 数据库中的@ForeignKey 和@Relation 注释有什么区别?
- java - 使用类
作为Java中注释的属性 - image - 如果使用 PowerShell 发送邮件,Outlook 会阻止签名中的链接图像
- python - 找不到 Jupyter 命令“jupyter-lab”
- sql - 我们如何获得 15 分钟的时间间隔
- python - Python - 将值附加到列表