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

create a single regex that creates group matches for dest_ip and dest_port

我目前正在为我的工作应对 regex/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>.+?)"

谁能给我指出正确的方向或发送一些可以给我示例的文件。

在您当前的模式中,您使用命名的捕获组,但您没有考虑名称 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