创建一个单一的正则表达式,为dest_ip和dest_port创建组匹配

问题描述 投票:1回答:1

我正在为我的工作正在研究正则表达式/ Splunk挑战。挑战是创建一个单一的正则表达式,从该示例日志文件为dest_ip和dest_port创建组匹配。

> Jan 15 15:16:11 10.0.5.9 Jan 15 15:16:11 ff:ff:00:01 nfc_id=20067 exp_ip=10.0.5.25 input_snmp=2 output_snmp=7 protocol=17 src_ip=43.152.96.179 src_host="unknown" src_port=1049 dest_ip=40.169.38.123 dest_host=unknown dest_port=137 tcp_flag=...... packets_in=131 bytes_in=22078 src_tos=0 dest_tos=0 src_asn=65535 dest_asn=65535 flow_count=1 percent_of_total=10.626 flow_smpl_id=2 t_int=30015  =24520

这是我创建的正则表达式,但似乎不对。正则表达式应该是提取dest_ip和dest_port字段

^"(?P<dest_ip>.+?)","(?P<dest_port>.+?)"

有人能指出我正确的方向,或发送一些可以给我举例的文件。

regex splunk
1个回答
0
投票

在您当前的模式中,您使用命名捕获组,但您没有考虑名称dest_ipdest_port本身。

请注意,^断言字符串的开头,并且您的示例数据不包含您在模式中使用的","

您可以使用\S+匹配等号后的非空格字符,并在其间匹配非贪婪的方式.*?以获取dest_ip:和dest_port的命名捕获组:

\bdest_ip=(?P<dest_ip>\S+).*?\bdest_port=(?P<dest_port>\S+)

说明

  • \bdest_ip=按字面意思匹配并使用单词边界来防止dest_ip成为更大单词的一部分
  • (?P<dest_ip>\S+)命名捕获组dest_ip,捕获匹配非空白字符的1倍以上
  • .*?匹配任何字符,除了换行符0+次非贪婪
  • \bdest_port=按字面意思匹配并使用单词边界来防止dest_port =成为更大单词的一部分
  • (?P<dest_port>\S+)命名捕获组dest_port,捕获匹配非空白字符的1倍以上

Regex demo

© www.soinside.com 2019 - 2024. All rights reserved.